douzai8285 2013-08-03 04:34
浏览 45

JQuery $ .post,PHP返回空白的POST变量

I cannot seem to get this to work. I have been researching this all day on Stack overflow and on the JQuery doc website itself, and no matter what i do, i cannot seem to get this to work.

Here is the HTML form (and the javascript for the JQuery post function):

<script>
function checkForm(){
// variable to hold request
var request;
// bind to the submit event of our form
$("#rchar").submit(function(event){
    // abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);
    // let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // fire off the request to /form.php
    request = $.ajax({
    url: 'postregister.php',
    type: "POST",
    data: serializedData,
    success: function(result){
        console.log(result);
    },
    error: function(){
        console.log('error');
    }   
});

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
    $("#navigation").html(serializedData);
    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });

    // prevent default posting of form
    event.preventDefault();
});
}
</script>
<form id="rchar" method="POST" action="postrenamecharacter.php">
<h3>Test Form!</h3>
<table id="leftalignment1">
<tbody>
<tr><td>Name: </td><td><input type="text" size="35px" id="name" value="" placeholder="Name" name="name"><input type="hidden" value="Test" id="hidden" name="hidden"></td></tr>
<tr><td>Password:</td><td><input type="password" size="35px" placeholder="Password" id="password" name="password"></td><td></td></tr>
</tbody>
</table><br><br>
<input action="submit" value="  Register  " id="submit" type="submit"><br>
</form>

So the response will be posted in the Java terminal and the JQuery POST variables will be loaded into the navigation bar. The navigation bar has the correct variables by reading this:

"name=Jeremy&hidden=Test&password=thisisatest"

Here is the PHP function called "postregister.php"

<?php
$oldname = $mysqli->real_escape_string($_POST['hidden']);
$password = $mysqli->real_escape_string($_POST['password']);
$title = "Your password is: ";
echo $title . "<br>" . $password . "<br>Your old name was: <br>" . $oldname;
?>

The issue is that the PHP code only returns this:

"Your password is: <br><br>Your old name was: <br>"

So obviously, the PHP variables are not being passed on to the PHP POST form from JQuery. Any help guys? I would really, REALLY appreciate it!

Thanks ^_^

  • 写回答

1条回答 默认 最新

  • doulu6314 2013-08-03 08:24
    关注

    You did not even call the function checkForm() method so I am not sure how your code works but I used $(document).ready(function()

    The following is the script:

    $(document).ready(function() {
        $("#rchar").submit(function(event){
            alert("hahahaha");
            var request;
            // abort any pending request
            if (request) {
                request.abort();
            }
            // setup some local variables
            var $form = $(this);
            // let's select and cache all the fields
            var $inputs = $form.find("input, select, button, textarea");
            // serialize the data in the form
            var serializedData = $form.serialize();
    
            // fire off the request to /form.php
            request = $.ajax({
            url: 'postregister.php',
            type: "POST",
            data: serializedData,
            success: function(result){
                console.log(result);
            },
            error: function(){
                console.log('error');
            }   
        });
    
            // callback handler that will be called on success
            request.done(function (response, textStatus, jqXHR){
            $("#navigation").html(serializedData);
            });
    
            // callback handler that will be called on failure
            request.fail(function (jqXHR, textStatus, errorThrown){
                // log the error to the console
                console.error(
                    "The following error occured: "+
                    textStatus, errorThrown
                );
            });
    
            // prevent default posting of form
            event.preventDefault();
        });
    });
    


    The following is the html:

     <body>
        <form id="rchar" method="POST" action="postregister.php">
        <h3>Test Form!</h3>
        <table id="leftalignment1">
        <tbody>
        <tr><td>Name: </td><td><input type="text" size="35px" id="name" value="" placeholder="Name" name="name"><input type="hidden" value="Test" id="hidden" name="hidden"></td></tr>
        <tr><td>Password:</td><td><input type="password" size="35px" placeholder="Password" id="password" name="password"></td><td></td></tr>
        </tbody>
        </table><br><br>
        <input action="submit" value="  Register  " id="submit" type="submit"><br>
        </form>
    </body>
    


    The following is the php script:

    <?php
      $oldname = $_POST['hidden'];
      $password = $_POST['password'];
      $title = "Your password is: ";
      echo $title . "<br>" . $password . "<br>Your old name was: <br>" . $oldname;
    ?>
    

    I tested it and I am able to get the value from ajax to the php page

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序