【发布时间】: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