PHP表单无法正常工作

时间:2017-01-09 16:19:51

标签: php html ajax

我的网站上有一个PHP表单,并设法使其正常运行。问题是它向我的HTML文件发回错误。我已经检查了网络邮件中的邮件,结果很好。

我认为这可能与使用XAMPP的控制台中的这一行有关,它显示:

Warning</b>:  mail() expects parameter 3 to be string, object given in 

mail('emails.contactus', $data , function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject)
    {
        $message->to($toEmail, $toName);

        $message->from($fromEmail, $fromName);

        $message->subject($subject);
    });

我也可以看到这个错误:

Undefined variable: data in

我没有邮件服务器,所以很难在XAMPP上完全调试。但是使用现场网站邮件会通过。

HTML:

<div class="col-md-8 contact-form-wrapper">

                <div class="alert alert-style-1 information-box-home success-alert">
                  <div class="alert-icon"><i class="ico-ok"></i></div><!-- end of alert thumb -->
                  <div class="alert-contents">
                    <h6 class="alert-title">Success Message</h6>
                    <p>Thank you for contacting us, you will hear from us soon!</p>
                  </div><!-- end of alert contents -->
                </div><!-- end of alert -->

                <div class="alert alert-style-1 information-box-home fail-alert">
                  <div class="alert-icon"><i class="ico-cancel"></i></div><!-- end of alert thumb -->
                  <div class="alert-contents">
                    <h6 class="alert-title">Failure Message</h6>
                    <p>Your message has not come through please contact  for more details</p>
                  </div><!-- end of alert contents -->
                </div><!-- end of alert -->

                <form action="contact-form.php" method="POST" class="contact-form">
                  <ul class="row">
                    <li class="col-md-6 form-item">
                      <label for="contact-name"><i class="ico-male"></i></label>
                      <input type="text" name="contact-name" class="contact-name" id="contact-name" value="Your Name" onblur="if(this.value=='')this.value='Your Name'" onfocus="if(this.value=='Your Name')this.value=''">
                    </li>
                    <li class="col-md-6 form-item">
                      <label for="contact-email"><i class="ico-email"></i></label>
                      <input type="email" name="contact-email" class="contact-email" id="contact-email" value="Your Email" onblur="if(this.value=='')this.value='Your Email'" onfocus="if(this.value=='Your Email')this.value=''">
                    </li>
                    <li class="col-md-12 form-item">
                      <label for="contact-message"><i class="ico-bubble"></i></label>
                      <textarea name="contact-message" class="contact-message" id="contact-message" data-placeholder="Your message"></textarea>
                    </li>
                    <li class="col-md-12 form-item">
                      <input type="submit" name="contact-btn" class="contact-btn general-link" id="contact-btn" value="Send Your Message">
                    </li>
                  </ul>
                </form><!-- end of contact form -->
              </div><!-- end of contact form wrapper -->

Ajax:

 <!-- Ajax Contact Form -->
      jQuery(document).ready(function($){
        // send message
        var form = $(".contact-form");
        $('.fail-alert').hide();
        $('.success-alert').hide();

        form.on( "submit", function( event ) { 
          $(".contact-btn").button('loading');

          event.preventDefault();

          $.ajax({
            type: "POST",
            url: form.attr( 'action' ),
            data: form.serialize(),
            success: function( response ) {
              console.log(response)
              if(response == "success"){
                $('.fail-alert').slideUp();
                $('.success-alert').slideDown();
              }
              else{
                $('.success-alert').slideUp();
                $('.fail-alert').slideDown();
              }
              console.log( response );
              $(".contact-btn").button('reset');
            }
          });

        });
      });

PHP:

<?php

if(!isset($_POST['submit'])) {

    $fromEmail      =  strip_tags($_POST['contact-email']);
    $fromName       =  strip_tags($_POST['contact-name']);
    $themessage     =  strip_tags($_POST['contact-message']);
    $themessage     =  $themessage."The Sender Is ( ".$fromName." )" ;

    $toEmail = 'email@email.com';
    $toName = 'Name';
    $subject = 'Enquiry';

    mail('emails.contactus', $data , function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject)
    {
        $message->to($toEmail, $toName);

        $message->from($fromEmail, $fromName);

        $message->subject($subject);
    });

$headers = 'From:' .$fromName . "\r\n" .
    'Reply-To:' .$fromEmail. "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail($toEmail, $subject, $themessage, $headers))
{
      // Send
echo "success";

}
else{ echo "An error has be occured"; 

}
}

?>

2 个答案:

答案 0 :(得分:0)

$data未在您的示例代码中定义,并且您将其作为第二个参数传递,第二个参数应该是主题行,不确定您是否先在页面中设置它并且您已经只是不包括在这里?

您可以使用占位符来测试它,例如:

$data = 'My subject line';

答案 1 :(得分:0)

你的剧本出现了一些问题。

您正在调用邮件功能两次,您在日志中获得的错误来自您对mail的第一次调用,其中未定义的变量为第二个参数,第三个参数类型不正确:

mail('emails.contactus', $data , function($message) use ($toEmail, ...)
{
    $message->to($toEmail, $toName);

    $message->from($fromEmail, $fromName);

    $message->subject($subject);
});

由于该错误只是一个警告,脚本会继续并通过第二次调用发送邮件mail()

if(mail($toEmail, $subject, $themessage, $headers))

我怀疑你想完全删除第一个mail()电话,它看起来应该是对其他一些恰好被称为邮件的功能的调用(或者你还没有改变它它的名称是正确的)