如何在激活提交按钮之前使用onkeyup同时验证多个输入值?

时间:2016-08-29 02:08:47

标签: javascript php jquery html forms

我想创建一个表单,其中提交按钮被禁用,直到表单准备好提交。不仅需要填写表格,还需要满足某些参数。密码必须匹配。电子邮件必须有效且尚未存在于数据库中。必须选择单选按钮。如何在每个upkey上同时发送所有输入的值。

这是我的index.php文件

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
        function validate_form(value){
            $.post("validate_form.php",{first_name:value},function(data){
                if (data=="Available"){
                    document.getElementById("mySubmit").disabled = false;
                } else {
                    document.getElementById("mySubmit").disabled = true;
                }
            });
        }; 
    </script>
</head>
<body>
    <form action="index.php" method="post">
        <input type="text" name="first_name" placeholder="First Name" onkeyup="validate_form(first_name.value)"><br>
        <input type="text" name="last_name" placeholder="Last Name"><br>
        <input type="text" name="email" placeholder="Email"><br>
        <input type="password" name="password" placeholder="Password"><br>
        <input type="password" name="password_repeat" placeholder="Re-enter Password"><br>
        <input type="radio" name="choice" value="Y" id="yes">Yes
        <input type="radio" name="choice" value="N" id="no">No
        <br>
        <input type="submit" name="submit" value="Sign Up" disabled id="mySubmit">
    </form>
</body>
</html>

这是我的validate_form.php文件

<?php

    $first_name = "";
    $last_name = "";
    $email = "";
    $password = "";
    $password_repeat = "";
    $answer = "";
    $go = array(0,0,0,0,0);

    if(isset($_POST['first_name'])){$first_name = $_POST['first_name'];}
    if(isset($_POST['last_name'])){$last_name = $_POST['last_name'];}
    if(isset($_POST['email'])){$email = $_POST['email'];}
    if(isset($_POST['password'])){$password = $_POST['password'];}
    if(isset($_POST['password_repeat'])){$password_repeat = $_POST['password_repeat'];}
    if(isset($_POST['choice'])){$answer = $_POST['answer'];}    

    //Does email already exist in database
    mysql_connect("localhost", "root", "password") or die (mysql_error ());
    mysql_select_db("database") or die(mysql_error());
    $strSQL = "SELECT * FROM users";
    $loop = mysql_query($strSQL);   
    $email_exists = 0;
    while($row = mysql_fetch_array($loop)) {
        if($row['email'] == $email){
            $email_exists = 1;
        }
    }

    //Is email valid

    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        $email_valid = 1;
    } else {
        $email_valid = 0;
    }

    //Check each parameter

    if(strlen($first_name) > 0){
        $go[0] = 1;
    }
    if(strlen($last_name) > 0){
        $go[1] = 1;
    }
    if($email_exists != 1 and $email_valid == 1){
        $go[2] = 1;
    }
    if(strlen($password) > 6 and $password == $password_repeat){
        $go[3] = 1;
    }
    if($answer != ""){
        $go[4] = 1;
    }

    if(array_sum($go) == 5){
        echo "Available";
    } else {
        echo "Form is not viable";
    } 

?>

我目前已将$ first_name发送到validate_form.php,但我不确定如何一次发送所有变量。任何帮助都会很棒。

0 个答案:

没有答案