[英]How to send a form with php / ajax without opening a new page when send
I have a little form and I'm trying to return a ajax error if the form is send or when something went wrong. 我有一个小表格,如果表格已发送或出现问题,我正在尝试返回一个ajax错误。
in connection.php I have a little script that submits the form 在connection.php中,我有一个提交表单的小脚本
if($_POST['postForm'] == 'newsletter'){
$newsletterSubscriber = new NewsletterSubscriber();
$newsletterSubscriber->set('CMS_newsletters_id', 2);
$newsletterSubscriber->set('created', date('Y-m-d H:i:s'));
$newsletterSubscriber->set('firstName', $_POST['voornaam']);
$newsletterSubscriber->set('lastName', $_POST['achternaam']);
$newsletterSubscriber->set('companyName', $_POST['beddrijfsnaam']);
$newsletterSubscriber->set('emailAddress', $_POST['email']);
$newsletterSubscriber->set('subscribed', 1);
$saved = $newsletterSubscriber->save();
$response = array('error_code'=>0,
'message'=>'subscriber added'
);
echo json_encode($response);
exit;
}
in my scripts.js I have my ajax: 在我的scripts.js中,我安装了ajax:
$.ajax({
type: "POST",
url: "connection.php",
data: {param1: 'aaa'},
dataType: JSON
})
.done( function(data){
if(data.error_code == 0) {
alert(data.message);
}
}
});
The error that I receive is: 我收到的错误是:
SyntaxError: Unexpected token '}'. Expected ')' to end a argument list.
Can somebody see what I'm doing wrong? 有人可以看到我在做什么吗?
Your JavaScript code has an error: 您的JavaScript代码有错误:
$.ajax({
type: "POST",
url: "connection.php",
data: {param1: 'aaa'},
dataType: JSON
})
.done( function(data){
if(data.error_code == 0) {
alert(data.message);
}
//You had an extra } here.
});
Try the following: replace done with the success property of the jquery ajax 请尝试以下操作:用jquery ajax的success属性替换完成
$.ajax({
type: "POST",
url: "connection.php",
data: {param1: 'aaa'},
dataType: JSON,
success:function(data){
if(data.error_code == 0) {
alert(data.message);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.