Ajax调用和返回响应

时间:2011-07-24 14:15:59

标签: php ajax

我正在尝试将地址添加到dotmailer中的列表(对于那些不熟悉的是像mailchimp这样的服务)我可以添加地址但是很难通过Ajax获得任何类型的返回状态。

我在php的表单页面中有以下内容

var emailEntered;

    $(document).ready(function() {

        $.fn.search = function() {
            return this.focus(function() {
                if( this.value == this.defaultValue ) {
                    this.value = "";
                }
            }).blur(function() {
                if( !this.value.length ) {
                    this.value = this.defaultValue;
                }
            });
        };
        $("#email").search();

        $("#sendButton").click(function() {
                $(".error").hide();
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                var emailaddressVal = $("#email").val();

                if(emailaddressVal == '') {
                    $("#message").html('<span class="error">Enter your email address before submitting.</span>');
                    return false; 
                }
                else if(!emailReg.test(emailaddressVal)) {
                    $("#message").html("<span class='error'>Please check your email address.</span>");
                    return false; 
                } 
                else {
                    emailEntered = escape($('#email').val());
                }

        });
        $('#signup').submit(function() {
            $("#message").html("<span class='error'>Adding your email address...</span>");
            $.ajax({
                url: 'dotmailerInput.php',
                data: 'ajax=true&email=' + emailEntered,
                success: function(msg) {
                    $('#message').html(msg);
                }
            });
            return false;
        });

    });
</script>

<form id="signup" action="dotmailer.php" method="get">
                <input type="email" name="email" id="email" class="textinput" value="Enter" />
                <input type="submit" id="sendButton" name="submit" class="textinput" value="" />
            </form>
            <div id="message"> </div>

在它引用的dotmailer.php页面中,我有以下内容。我可以看到它给了我一个“添加你的电子邮件地址”的回复,但在此之后没有别的,我说的电子邮件被正确添加。

$email = $_GET['email'];    
$username = ""; //apiusername
$password = ""; //api password      
$addressbookid = ;
$AudienceType = "Unknown";
$OptInType = "Unknown";
$EmailType = "Html";


try {

$client = new SoapClient("http://apiconnector.com/api.asmx?WSDL");
$contact = array("Email" => $email,"AudienceType" => $AudienceType, "OptInType" => $OptInType, "EmailType" => $EmailType, "ID" => -1);

$dotParams = array("username" => $username, "password" => $password, "contact" => $contact, "addressbookId" => $addressbookid);
$result = $client->AddContactToAddressBook($dotParams);
return "Success";
}
catch (Exception $e) {
return "Error";
}

非常感谢任何有关看什么或下一步的帮助或提示。

克里斯

1 个答案:

答案 0 :(得分:0)

尝试使用echo代替return,因为您处于PHP的顶层并且将使用此输出(而不是return为函数指定值)。

echo "Success";
}
catch (Exception $e) {
echo "Error";