我正在使用AJAX(jQuery)向PHP脚本提交表单。
下面是 JavaScript:
// Validate form fields
$(function () {
//Check to see if email is not empty
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var dataString = 'firstname=' + firstname + '&email=' + email + '&lastname=' + lastname;
$.ajax({
type: "POST",
url: "/",
data: dataString,
success: function () {
var counter = <?php echo($counter['count'] + 1); ?>
counter = numberWithCommas(counter);
$('#counter').html(counter);
$('#contact_form').html("<div id='message'></div>").fadeIn(1500, function () {
$('#message').html("<h2>Thanks " + (firstname) + "!</h2>").append("<p>We will be in touch soon.</p>");
});
}
});
return false;
});
在PHP脚本中,我有以下代码:
if (!empty($_POST)){
$adestra = $this->adestraAction('contact.create' , array('1', array('1.email' => $_POST['email'] , '1.forename' => $_POST['firstname'] , '1.surname' => $_POST['lastname'],'1.source' => 'signupbox' )));
$add = $this->addToListAction($adestra, $magazineID);
}
_POST美元的变量似乎没有被设定下来。谁能帮忙?
data: dataString : dataString,
这是一个语法错误。 我假设你真的想要:
data: dataString,
jQuery POST 请求中的数据必须是 Javascript 对象。
将其更改为:
data: {
'key' : 'value'
}
或者,如果 dataString 已经格式化为查询字符串,请参阅 Rocket 的答案。
请在此行之后调试/警报数据字符串
var dataString = 'firstname=' + firstname + '&email=' + email + '&lastname=' .....
查看是否正确填充了所有值? 这可能是问题所在