0

我通过 ajax 更新数据库没有什么问题,但我无法让它工作。我不知道我做错了什么。

这是我的php文件(sendata.php)

  <?php
       if ( isset($_POST['register']) && $userid != 0) 
         {$register = $_POST['register'] ;

            $sql2 =$db->setQuery("INSERT INTO ....  ");
                   $db->query() ;
           }

   ?>

这里是我的 javascript 代码

$(document).ready(function() {
    $('#register').click(function() {

        $.ajax({

            url: "sendata.php",
            data: {
                age: $('#age').val()
            },
            type: 'POST',

            success: function(msg) {
                alert("Data has been saved succefully");
                $('#mydata').html("<b> age: </b>" + age);
            }
        });

        return false;
    });
});

对我来说,当我点击注册按钮时,我只收到警报,该数据已成功保存,但是当我进入数据库查看时,那里根本没有记录。我做错了什么?

编辑 :

这是我的按钮

     <input type="submit" id="register" name="register" value="Save my Data" />
4

2 回答 2

1

sendata.php 检查是否设置if ( isset($_POST['register']) ...) 了“注册”:因此您必须在请求中设置变量“注册”(我修复了代码 - 见粗体):

$(document).ready(function() {
    $('#register').click(function() {

        $.ajax({

            url: "sendata.php",
            data: {
                age: $('#age').val(),
                register: "register"
            },
            type: 'POST',

            success: function(msg) {
                alert(msg);
                $('#mydata').html("<b> age: </b>" + age);
            }
        });

        return false;
    });
});

发送数据文件

if ( isset($_POST['register']) && $userid != 0) 
{
     $register = $_POST['register'] ;
     $sql2 =$db->setQuery("INSERT INTO ....  ");
     $db->query() ;
     echo "SUCCESS";
     exit(0);
 } 
 echo "FAILURE";
于 2012-11-19T19:58:57.123 回答
0

Javascript:

                    jQuery(document).ready(function($)
                    {
                    //prevent registering the event more than once 

                    //$('#register').off('click').on('click',(function(e) {

(1.) 我将从:

                    $('form').off('submit').on('submit',(function(e) {

或者 ..

                        $('input[type="submit"]').off('click').on('click',(function(e) {

然后 ..

                                $.ajax({
                                              //url to touch
                                            url: "sendata.php",
                                            data: {
                                                age: $('#age').val()
                                            },
                                            type: 'POST',
                                            //on fail
                                                        fail: function(data){
                                                         //regular javascript alert on error
                                                                    alert("Oppps! Data error");
                                                        },
                                            //on success
                                            success: function(data) {
                                                //regular javascript alert on success
                                                alert("Data has been saved succefully");

                                                //assuming this is a div with an id of mydata
                                                $('#mydata').html("<b> Age is: </b>" + data);
                                            }
                                        });

                                //prevents default action as we are not submiting the form but rather making use of AJAX to touch the php file
                                e.preventDefault();

                                });

                    });

发送数据文件

                                <?
                $msg= '';

                    if (  isset( $_POST['age'] )  ) 
                    {
                        $age = filter_input(INPUT_POST, "age", FILTER_SANITIZE_STRING); 
                //echo $age;

                //around this check if the insertion is correct and that you can talk to the db
                //db handling, insertion, etc..
                                  // $sql2 =$db->setQuery("INSERT INTO ....  ");
                                  // $db->query();

                        //$msg .= "Epic Win! $age";
                        $msg .= "$age";
                    }
                    else $msg .= 'Error, no age provided :( ';
                        //we are returning the data so we need to echo it as an output here.
                    echo $msg;

                ?>
于 2012-11-21T00:40:16.983 回答