douxun2023 2012-01-31 17:42
浏览 62
已采纳

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

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(); }
?>
  • 写回答

3条回答 默认 最新

  • doubu1853 2012-01-31 17:51
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料