簡體   English   中英

表單驗證規則將被忽略

[英]Form validation rules be ignored

我嘗試使用jquery插件實現表單驗證。 如果單擊提交按鈕,則除了執行php代碼以及創建cookie userid usernameusername外,什么都沒有發生。 添加$.ajax({ ,表單驗證規則將被忽略,並且重定向失敗,但是如果沒有$.ajax({則php代碼將無法工作。驗證,php代碼和重定向如何一起工作?

我的html代碼:

    <head>
        <meta charset="UTF-8" /> 
        <title>
        HTML Document Structure
        </title>    
        <link rel="stylesheet" type="text/css" href="style.css" />
        <link rel="stylesheet" href="themes/my-costum-theme.min.css" />
        <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
        <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>    
        <!-- Einstellungen zur Defintion als WebApp -->
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <script src="loadagain.js"></script>    
        <script>
        $(document).ready(function () {
            $('#myform').validate({ // initialize the plugin
                rules: {
                    username: {
                        required: true,
                        minlength: 2,
                        maxlength: 30
                    },
                    password: {
                        required: true,
                        minlength: 3,
                        maxlength: 30
                    }
                },
                submitHandler: function (form) { // for demo
                    $.ajax({
        type: 'post',
        url: 'loginprivate.php'    //url where you want to post the stuff.
        data:{
            username: 'root',
            password: ''
        },
        success: function(res){
            //here you will get res as response from php page, either logged in or some error.
             window.location.href = "http://localhost/loc/main.php";
        }
    });
                    return false; // for demo
                }
            });
        });
        </script>   
        </head>
        <body>
        <div class="ui-page" data-theme="b" data-role="page">
            <div data-role="header"><h1>localcorps</h1></div>
                <div id="wrapper1" style=" width: 90%; padding-right:5%; padding-left:5%" name="wrapper1">
                    <form  name="login-form" id="myform" class="login-form" action="./loginprivate.php" method="post">
                        <div class="header1"></div>
                            <div class="content1">
                                <div data-role="fieldcontain">
                                <label for="username">Username:</label>
                                <input name="username" type="text" class="input username" id="username"/>
                            </div>
                        </div>
                        <div data-role="fieldcontain">
                            <label for="password">Password:</label>
                            <input name="password" type="password" class="input password" id="password"/>          
                        </div>
                        <div class="footer">
                            <input type="submit" name="submit" value="Login" class="button"/>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        </body>

我的php代碼:

    if(isset($_POST["submit"]))
    {
            $hostname='localhost';
            $username='root';
            $password='';

            unset($_POST['password']);
            $salt = ''; 
            for ($i = 0; $i < 22; $i++) { 
                    $salt .= substr('./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', mt_rand(0, 63), 1); 
            }
            $_POST['password'] = crypt($_POST['password'],'$2a$10$'.$salt);
            $new = 0;
            try {
                    $dbh = new PDO("mysql:host=$hostname;dbname=search",$username,$password);
                    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
                    $sql = "INSERT INTO users (username, password)
                    VALUES ('".$_POST["username"]."','".$_POST["password"]."')";
                    if ($dbh->query($sql)) {
                            echo "New Record Inserted Successfully";
                    }
                    else{
                            echo "Data not successfully Inserted.";
                    }
                    $new = $dbh->lastInsertId();
                    $dbh = null;
            }
            catch(PDOException $e)
            {
                    echo $e->getMessage();
            }
            if ($new > 0) 
            {

                $t = time() + 60 * 60 * 24 * 1000;
                setcookie("username", $_POST['username'], $t);
                setcookie("userid", $new , $t);
            } 
        else
            {

            }
    }

因此,使用validate()內置函數更新ajax調用

<script>
    $(document).ready(function () {
        $('#myform').validate({ // initialize the plugin
            rules: {
                username: {
                    required: true,
                    minlength: 2,
                    maxlength: 30
                },
                password: {
                    required: true,
                    minlength: 3,
                    maxlength: 30
                }
            },
            submitHandler: function (form) {               
                // do other things for a valid form
                form.submit();     //will submit your form.
                return false; // disables default submit.
            }
        });
    });
</script>

有關更多信息,請在這里閱讀,尤其是示例2和3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM