即使 PHP 文件设置正确,也没有 Ajax 响应

发布于 2024-12-31 22:11:13 字数 3048 浏览 1 评论 0原文

我有一个简单的注册邮件列表表格。它将用户的电子邮件地址发送到 store-address.php 文件。我使用 jQuery 的 ajax 对象向 php 文件发送请求,然后接收响应。

问题是我没有从 php 文件得到响应。我尝试在请求中将缓存设置为 false。我还尝试通过 URL 发送信息,如下所示:

http://www.fifthtribe.com/inc/store-address.php?ajax=true&cache=false&email=test4%40gmail.com

当我这样做时,它可以工作并给出我一个回应。但是当我通过ajax 执行此操作时,它没有给我任何响应。这是来自 Firebug:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

这是我的代码片段:

HTML:

<div id="mlist">
<form id="mlist_form" method="POST" action="">
    <input type="text" id="email" name="email" placeholder="Email" />
    <input type="submit" id="submit_btn" value="Join" />
</form>
<div id="response"></div>
</div>

JQuery:

/* Add to mailing list */           
$("#mlist_form").submit( function(e){
    //$('#response').append('<div id="thanks-mce"><div id="mce-arrow"></div>Thanks for signing up!</div>');
    var email = escape( $('#email').val() );
    e.preventDefault();

    data = {
        "ajax" : "true",
        "email" : email,
        "cache" : "false"
}

    $.ajax({ 
            type: "POST",           
            url: 'inc/store-address.php',
            data: data,
            success: function( msg ){
                // successfully signed up
                $('#response').html( msg );
                $('#email').val('');
            },
            error: function( err ){
                // error while signing up
                $('#response').html('Error: Is your email correct?');
            }
    });

    return false;

});

PHP: 函数存储地址(){

    // Validation
    if(!$_GET['email']){ return "No email address provided"; } 

    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
        return "Email address is invalid"; 
    }

    require_once('MCAPI.class.php');
    // grab an API Key from http://admin.mailchimp.com/account/api/
    $api = new MCAPI('xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-us4');

    // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
    // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
    $list_id = "xxxxxxxx";

    if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
        // It worked!   
        return 'Success! Check your email to confirm sign up.';
    }else{
            // An error ocurred, return error message   
            return 'Error: ' . $api->errorMessage;
    }

}

    // If being called via ajax, autorun the function
    if($_GET['ajax']){ echo storeAddress(); }
?>

I have a simple sign up mailing list form. It sends the user's email address to a store-address.php file. I use jQuery's ajax object to send a request to the php file and then receive a response.

The problem is I am not getting a response from the php file. I tried setting the cache to false in the request. I also tried send the information through the URL like so:

http://www.fifthtribe.com/inc/store-address.php?ajax=true&cache=false&email=test4%40gmail.com

When I do it that way it works and gives me a reponse. But when I do it through ajax it doesn't give me a response. This is from Firebug:

enter image description here

enter image description here

enter image description here

And here's snippets from my code:

HTML:

<div id="mlist">
<form id="mlist_form" method="POST" action="">
    <input type="text" id="email" name="email" placeholder="Email" />
    <input type="submit" id="submit_btn" value="Join" />
</form>
<div id="response"></div>
</div>

JQuery:

/* Add to mailing list */           
$("#mlist_form").submit( function(e){
    //$('#response').append('<div id="thanks-mce"><div id="mce-arrow"></div>Thanks for signing up!</div>');
    var email = escape( $('#email').val() );
    e.preventDefault();

    data = {
        "ajax" : "true",
        "email" : email,
        "cache" : "false"
}

    $.ajax({ 
            type: "POST",           
            url: 'inc/store-address.php',
            data: data,
            success: function( msg ){
                // successfully signed up
                $('#response').html( msg );
                $('#email').val('');
            },
            error: function( err ){
                // error while signing up
                $('#response').html('Error: Is your email correct?');
            }
    });

    return false;

});

PHP:
function storeAddress(){

    // Validation
    if(!$_GET['email']){ return "No email address provided"; } 

    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
        return "Email address is invalid"; 
    }

    require_once('MCAPI.class.php');
    // grab an API Key from http://admin.mailchimp.com/account/api/
    $api = new MCAPI('xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-us4');

    // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
    // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
    $list_id = "xxxxxxxx";

    if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
        // It worked!   
        return 'Success! Check your email to confirm sign up.';
    }else{
            // An error ocurred, return error message   
            return 'Error: ' . $api->errorMessage;
    }

}

    // If being called via ajax, autorun the function
    if($_GET['ajax']){ echo storeAddress(); }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿哥们儿 2025-01-07 22:11:13

您意识到您的 PHP 脚本正在使用 GET 方法,但您的 jQuery 代码正在使用 POST 方法,对吗?

如果信息被发布到 PHP,PHP 将需要使用 $_POST 来检索它。这解释了为什么使用 $_GET 的 URL 方法可以工作,但 jQuery POST 却不行。

祝你好运!

You realize that your PHP script is using GET method but your jQuery code is using the POST method right?

If the information is being posted to PHP, PHP will need to use $_POST to retrieve it. This explains why the URL method using $_GET works but the jQuery POST doesn't.

Good luck!

往昔成烟 2025-01-07 22:11:13

看起来您正在使用 $_GET 而不是 $_POST。尝试回显 $_REQUEST 的内容以查看其中包含的内容。

It looks like you're using $_GET instead of $_POST. Try echoing out the contents of $_REQUEST to see what that holds.

恍梦境° 2025-01-07 22:11:13

调试你的脚本!

在脚本的成功和错误部分放置警报,然后您就会知道 AJAX 是否正常工作。

如果没有,您可以继续查看文档并查看问题出在哪里。

另外,这里的错误很简单。您在 PHP 中使用 $_GET 并且使用 AJAX 发布数据,这不会显示错误。尽管 PHP 文档不会处理您的请求,因为它没有提供任何参数。

Debug your script!

Place an alert in the success and error parts of your script and then you will know whether the AJAX is working.

If not, you can then work your way up the document and see where the problem is.

In addition, the error here is quite simple. You are using $_GET in PHP and you are POSTING your data using AJAX, this will not show an error. Although the PHP document will not process your request because it is not being fed any parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文