繁体   English   中英

发送时如何在不打开新页面的情况下使用php / ajax发送表单

[英]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.

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