Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ajax提交数据以进行刷新_Php_Jquery_Mysql - Fatal编程技术网

Php 使用ajax提交数据以进行刷新

Php 使用ajax提交数据以进行刷新,php,jquery,mysql,Php,Jquery,Mysql,我已经成功地使用ajax通过ajax(一种联系人表单)在线刷新。我在处理发送到mysql的数据时遇到了一些困难。我很感激到目前为止,我所拥有的并不是要通过ajax刷新的,所以可能需要做一些工作。这是我得到的。。。感谢您的帮助 形式 PHP 您丢失了来自服务器的数据(删除服务器SKcript中的HTML标记): $.ajax({ 类型:“POST”, url:“mailing_list_add.php”, 数据:dataString, 成功:函数(数据){ $('#email_add').html

我已经成功地使用ajax通过ajax(一种联系人表单)在线刷新。我在处理发送到mysql的数据时遇到了一些困难。我很感激到目前为止,我所拥有的并不是要通过ajax刷新的,所以可能需要做一些工作。这是我得到的。。。感谢您的帮助

形式

PHP


您丢失了来自服务器的数据(删除服务器SKcript中的HTML标记):

$.ajax({
类型:“POST”,
url:“mailing_list_add.php”,
数据:dataString,
成功:函数(数据){
$('#email_add').html(“”);
$('#response').html(“已成功添加到数据库。”)
.hide()
.fadeIn(2500,函数(){
$('#response').html(数据);
}); 
}  
});  

有一些问题,例如:

  • 您需要将javascript附加到事件(表单提交),因此第一部分类似于:

    $('form')。提交(函数(){

  • 您需要发送所有(正确的…)表单值,而不仅仅是php工作的电子邮件地址:

    $('form')。提交(函数(){


  • 好的,我想我已经尝试过调整其他东西来工作,但这不是最好的方法。谢谢。令人恼火的是,我的PHP部分工作正常,代码也工作正常。除了JQuery,我无法停止页面刷新。令人沮丧。@Andy你为什么不注释掉整个ajax调用,然后从表单事件处理程序开始呢?请注意您需要从表单事件处理程序返回false,以避免表单以正常方式提交。我已经更新了上述表单和jquery的代码。ajax调用被注释掉,现在url字符串包含正在提交的电子邮件和另一个指定这是提交地址的字段。
    <form name="email_list" action="">
    
    <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" name="submit" value="Submit Form" class="email_submit"></p>
    </form>
    
    $(function() {    
    $('.email_submit').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 = $(this).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
    // connects the database access information this file
    include("mailing_list_include.php");
    
    // the following code relates to mailing list signups only
    if (($_POST) && ($_POST["action"] == "sub")) {
    
    if ($_POST["email"] == "") {
            header("Location: mailing_list_add.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.";
        }
    }
    }
    
    ?>
    <html>
    <?php echo "$display_block";?>
    </html>
    
    $.ajax({  
            type: "POST",  
            url: "mailing_list_add.php",  
            data: dataString,  
            success: function(data) {  
                $('#email_add').html("<div id='response'></div>");  
                $('#response').html("<h2>Successfully added to the database.")                      
                .hide()  
                .fadeIn(2500, function() {  
                    $('#response').html(data);  
                }); 
            }  
        });  
    
       var dataString = $(this).serialize();