如何在不重新加载页面的情况下直接验证数据库中的输入


How can I validate inputs directly from the database without page reloading?

我只是一个PHP初学者,现在我想学习JQUERY,在我的学习过程中,我练习验证输入,通常我只使用纯PHP代码验证我的输入,每次验证输入时,页面都会重新加载,现在我想要改进我发现的一些文章http://api.jquery.com/jQuery.ajax/,http://api.jquery.com/jQuery.post/(不能发布其他链接)但我更困惑,因为他们有不同的方法,我想使用JQUERY教程中的方法,但我没有找到任何好的教程,JQUERY的网站上也没有使用数据库的教程,通常我的代码是这样的:

<form method="post">
<label for="Username">Username:</label>
<input id="Username" type="text" name="username">
<?php
session_start();
if(isset($_SESSION['msg'])){
$msg=$_SESSION['msg'];
echo '<label for="Username">'.$msg.'</label>';
?>
<input type="submit" name="reg">
</form>
<?php
if(isset($_POST['reg'])){
$result=//check username from database here
if($result){
$_SESSION['msg']='username not available.';
}
else {
$_SESSION['msg']='username available.';
}
}
?>

现在我想学习如何在不重新加载页面的情况下直接验证数据库中的输入?我不知道该从哪里开始,也不知道该在代码中添加什么。任何帮助、建议或建议对我来说都是一个很大的帮助:)

首先,在表单中添加onSubmit函数

 <form name='myform' type='POST' action='http://www.action.fr' onSubmit="return check_form()">

您可以像一样在ajax中完成此操作

function check_form()
 {
    var user = $('#Username').val(); // Username is the id of your input
    var password = $('#password').val(); // password is the id of your input
    $.ajax(
   {
     type:"POST", // or get as you want
     url:"myfile.php", // it is the php file which can do the job
     data: "user="+user+"&password="+password, // the param to send to your file, 
     success:function(msg)
     {
          ;// msg is the result of your 'myfile.php', everything you write is in the msg var
     }
  });
}

在php文件中,您可以获得如下数据:

 $user = $_POST['user'];
 $password = $_POST['password'];
 // if your type is get then use $_GET instead of $_POST

如果您对我的代码有任何问题,请告诉我。

编写验证脚本时,就好像在等待页面刷新一样。不要输出错误消息,而是将它们放在JSON数组中并打印JSON数据。然后从AJAX函数调用脚本。就这么简单。

<?php
// validate.php
$sampleInput_number = isset($_POST['sampleInput_number']) ? $_POST['sampleInput_number'] : "";
$errors = array();
if (trim($sampleInput_number) == "" || !is_numeric(trim($sampleInput_number)) {
    $errors[] = "SampleInput_number must be a number!";
}
// sample input must also match a value from the database
if (!matchesDBValue($sampleInput_number)) {
    $errors[] = "SampleInput_number must match a value from the database!";
}
function matchesDBValue($value) {
    $retval = false;
    // compare to db values here...
    return $retval;
}
echo json_encode($errors);

你的表格看起来像这样:

<form action="" method="post" id="theForm">
    <input type="text" name="sampleInput_number" id="sampleInput_number" />
    <input type="button" id="formSubmit" value="Submit" />
</form>

您的javascript看起来是这样的:

<script language="javascript" type="text/javascript">
    $(#formSubmit).on("click", function() {
        $.post("validate.php", 
            {
                sampleInput_number: $("#sampleInput_number").val()
            }, function(data) {
                // check returned json data
                // perform action based on results
                if (no_errors) {
                    $("#theForm").submit();
                }
            }, "json"
        );
    });
</script>