![](/img/trans.png)
[英]how to send form values to php using AJAX without reloading page?
[英]How to send a form with php / ajax without opening a new page when send
我有一個小表格,如果表格已發送或出現問題,我正在嘗試返回一個ajax錯誤。
在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;
}
在我的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);
}
}
});
我收到的錯誤是:
SyntaxError: Unexpected token '}'. Expected ')' to end a argument list.
有人可以看到我在做什么嗎?
您的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.
});
請嘗試以下操作:用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.