繁体   English   中英

将Ajax表单提交到PHP脚本时出现问题

[英]Problems submitting ajax form to PHP script

我正在使用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
}

?>

我不确定这里发生了什么,但似乎序列化的数据未正确访问。 这是我的序列化数据的控制台日志的片段。

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

您在提供错误的代码上方的行中缺少PHP脚本中的分号。

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

通常,当您遇到这样的语法错误时,它实际上在上面的行中(有时甚至更高)。 PHP解析器认为上一条语句未完成(由于缺少分号),因此继续分析同一条语句。 那时,下一条语句的意外开始使它感到困惑。 因此,当您在第10行收到这样的消息时,请确保也查看第9行或第8行。

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

$subject  =   'A enquiry for The Retros!'

应该:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM