试图将数据发送到mysql,接收回调问题

时间:2012-02-24 14:16:16

标签: php jquery ajax

这是一个邮件列表脚本。它本身没有jquery工作,但我试图使它适应与ajax一起使用。但是,没有成功。当$ .sql部分被注释掉时,它会成功返回url字符串中的变量。但是,当我取消注释js文件的那一部分并将PHP引入事物时,它只是刷新页面,电子邮件地址仍在输入框中。 PHP本身就是这样,所以我不知道我哪里出错了。这就是我所拥有的...任何帮助都将不胜感激。

表格:

<form name="email_list" action="" id="maillist_form">

<p><strong>Your Email Address:</strong><br/>
<input type="text" name="email" id="email" size="40">
<input type="hidden" name="sub" id="sub" value="sub">

<p><input type="submit" value="Submit Form" class="email_submit"></p>
</form>

JQuery:

$(function() {    
$('#maillist_form').submit(function() { 

    var email = $("input#email").val();  
                if (name == "") { 
                $("input#email").focus();  
                return false;
            }  
    var sub = $("input#sub").val();  
                if (name == "") {  
                $("input#sub").focus();  
                return false;
            }       

    var dataString = $("#maillist_form").serialize();

    //alert (dataString);return false;  
    $.ajax({  
        type: "POST",  
        url: "mailing_list_add2.php",  
        data: dataString,  
        success: function() {  
            $('#display_block')                      
            .hide()  
            .fadeIn(2500, function() {  
                $('#display_block');  
            }); 
        }  
    }); 
    return false; 
});

});

PHP:

<?php
// connects the database access information this file
include("mailing_list_include.php");

// the following code relates to mailing list signups only
if (($_POST) && ($_POST["sub"] == "sub")) {

if ($_POST["email"] == "") {
        header("Location: mailing_list_add2.php");
        exit;
    } else {
        // connect to database
        doDB();

        // filtering out anything that isn't an email address
        if ( filter_var(($_POST["email"]), FILTER_VALIDATE_EMAIL)  == TRUE) {
            echo '';
        } else {
            echo 'Invalid Email Address';
            exit;
        }

        // check that the email is in the database
        emailChecker($_POST["email"]);

        // get number of results and do action
        if (mysqli_num_rows($check_res) < 1) {
            // free result
            mysqli_free_result($check_res); 

            // cleans all input variables at once
            $email = mysqli_real_escape_string($mysqli, $_POST['email']);

            // add record
            $add_sql =  "INSERT INTO subscribers (email) VALUES('$email')";
            $add_res =  mysqli_query($mysqli, $add_sql)
                        or die(mysqli_error($mysqli));
            $display_block = "<p>Thanks for signing up!</p>";

            // close connection to mysql
            mysqli_close($mysqli);
        } else {
            // print failure message
            $display_block = "You're email address - ".$_POST["email"]." - is already subscribed.";
    }
}
}

?>

我不会在这里放入包含代码,因为我认为它是正确的 - 除非引入jquery意味着这也需要进行调整。

2 个答案:

答案 0 :(得分:1)

你的AJAX没有收回结果:

$.ajax({  
    type: "POST",  
    url: "mailing_list_add2.php",  
    data: dataString,  
    success: function(response) {  
        $('#display_block')                      
        .hide()  
        .fadeIn(2500, function() {  
            $('#display_block').html(response);   //just an example method. 
       //Are you sure the selector is the same? 
       //Can also be $(this).html(response);
    }  
}); 

正如gdoron所说,没有“名称”变量。也许你的意思分别是“电子邮件”和“子”?

PHP响应也没有回应。只是说:

echo $display_block;

答案 1 :(得分:0)

您不会回显服务器中的数据,也不会尝试在成功回调中获取数据,而fadeIn回调只有一个选择器,

您检查错误的变量:

var email = $("input#email").val();  
            if (name == "") {  // Didn't you mean email?
            $("input#email").focus();  
            return false;
        }  
var sub = $("input#sub").val();  
            if (name == "") {  // Didn't you mean sub?
            $("input#sub").focus();  
            return false;
        } 

如何运作?