【问题标题】:Problems submitting ajax form to PHP script将 ajax 表单提交到 PHP 脚本时出现问题
【发布时间】:2015-09-10 12:52:37
【问题描述】:

我正在使用 jquery 序列化发送表单数据,如下所示:

$.ajax({
            url: $form.attr('action'),
            type: 'POST',
            datatype : 'html',
            data: $('#form').serialize();,
            success: function(response) {
                if (response.status === "success") {
                    $('.result').text("Success!!");
                } else if (response.status === "error") {
                    $('.result').text("Error!!");
                }
            }
        });

这已成功传递到 PHP 脚本,但我的 PHP 中出现以下错误。

Parse error: syntax error, unexpected '$name' (T_VARIABLE) in C:\Users\Jberke\Documents\Projects\theRetros\src\process.php on line 10

我的PHP脚本如下:

<?php
$autoResponse = true; //if set to true auto response email will be sent, if you don't want autoresponse set it to false
$autoResponseSubject = "Demo Contact Form"; 
$autoResponseMessage = "Hi, thank you testing the JQuery Contact Form Demo.";
$autoResponseHeaders = "From: email_from@yourWebsite.com";  

//we need to get our variables first
$email_to =   'jamie_b25@hotmail.com';
$subject  =   'A enquiry for The Retros!'
$name     =   $_POST['name'];
$email    =   $_POST['email'];
$message  =   $_POST['message'];

$body = "From: $name \r\nMessage: \r\n$message";

/*the $header variable is for the additional headers in the mail function,
 we are asigning 2 values, first one is FROM and the second one is REPLY-TO.
 That way when we want to reply the email gmail(or yahoo or hotmail...) will know
 who are we replying to. */
$headers  = "From: $email\r\n";
$headers .= "CC: test@test.com\r\n";
$headers .= "Reply-To: $email\r\n";


if(mail($email_to, $subject, $body, $headers)){
    if($autoResponse === true){
        mail($email, $autoResponseSubject, $autoResponseMessage, $autoResponseHeaders);
    }
    echo 'success'; // we are sending this text to the ajax request telling it that the mail is sent..
}else{
    echo 'error';// ... or this one to tell it that it wasn't sent
}

?>

我不确定这里发生了什么,但似乎没有正确访问序列化数据。这是我序列化数据的控制台日志的 sn-p。

name=Jamie+Berke&email=jamie_b25%40hotmail.com&message=testing+123

【问题讨论】:

    标签: php jquery ajax forms serialization


    【解决方案1】:

    您在给出错误的那一行的 PHP 脚本中缺少一个分号。

    $subject  =   'A enquiry for The Retros!'  // <--- Here a ; should be added.
    $name     =   $_POST['name'];
    $email    =   $_POST['email'];
    

    通常,当您遇到这样的语法错误时,它实际上是在上面的行中(有时甚至更高)。 PHP 解析器认为前面的语句没有完成(因为缺少分号),并继续解析相同的语句。那时它会被下一个语句的意外开始弄糊涂。因此,当您在第 10 行收到这样的消息时,请务必查看第 9 行或第 8 行。

    【讨论】:

    • 谢谢你!虽然我的脚本仍然没有提交表单或返回成功或失败数据。不过这次我没有收到任何 PHP 错误,所以发生了其他事情。
    【解决方案2】:

    很简单,你在这里忘记了分号:

    $subject  =   'A enquiry for The Retros!'
    

    应该是:

    $subject  =   'A enquiry for The Retros!';
    

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多