0

我似乎无法让它发挥作用。我整天都在研究 Stack overflow 和 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 表单。有帮助吗?我真的,真的很感激!

谢谢^_^

4

1 回答 1

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 页面获取值

于 2013-08-03T08:24:31.170 回答