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

时间:2013-08-03 04:34:19

标签: php javascript jquery post .post

我似乎无法让这个工作。我一整天都在研究Stack溢出和JQuery doc网站本身,无论我做什么,我似乎无法让它工作。

这是HTML表单(以及JQuery post函数的javascript):

<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>

因此响应将发布在Java终端中,JQuery POST变量将加载到导航栏中。导航栏具有正确的变量,请阅读:

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

这是名为“postregister.php”的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;
?>

问题是PHP代码只返回:

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

很明显,PHP变量没有从JQuery传递到PHP POST表单。有帮助吗?我真的,真的很感激!

谢谢^ _ ^

1 个答案:

答案 0 :(得分:0)

您甚至没有调用函数 checkForm()方法,因此我不确定您的代码是如何工作的,但我使用了 $(document).ready(function()

以下是脚本:

$(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();
    });
});


以下是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>


以下是php脚本:

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

我测试了它,我能够从ajax获取值到php页面