简体   繁体   English

jquery表单验证和php脚本在一个页面中

[英]jquery form validation and php script in one page

after implementing a jquery form validation and redirecting with function btn_onclick() { window.location.href = "http://localhost/loginprivate.php";} from index.php to loginprivate.php my webapps php script wont be executed. 在实现jquery表单验证并使用function btn_onclick() { window.location.href = "http://localhost/loginprivate.php";}index.php loginprivate.phploginprivate.php我的webapps php脚本将不会被执行。 The user become trough a javascript function and window.location.href from index.php redirected loginprivate.php you can see here. 用户通过javascript函数和index.php重定向的loginprivate.php window.location.href ,你可以在这里看到。 After the first pageload the page become loaded again with <script src="loadagain.js"></script> this works fine too. 在第一次页面加载后,页面再次使用<script src="loadagain.js"></script>加载,这也可以正常工作。

Now the problem is that if I click the submit button the php code wont become executed, that can I see because no cookies become created, and the user become redirected to index.php . 现在的问题是,如果我单击提交按钮,php代码将不会被执行,我可以看到,因为没有创建cookie,并且用户被重定向到index.php

my php code: 我的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
            {

            }
    }

my html code: 我的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').ajaxForm();
            $('#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: 'maxicora123'
        },
        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>

According to your comment, I get now only this "Array ( )" on my screen. 根据你的评论, 我现在只在屏幕上显示“Array()”。 it means that you ain't posting anything to PHP page. 这意味着您没有向PHP页面发布任何内容。

That's just because, you're redirecting the control to PHP page instead of submitting the form. 这只是因为,您redirecting控件redirectingPHP页面而不是submitting表单。

so, you can validate the code using jQuery and on successful validation do a ajax post request instead of shifting the control. 所以,你可以使用jQuery验证代码,并在成功验证时执行ajax post请求而不是移动控件。 window.location.href = "http://localhost/lak/main.php";

instead of shifting control, do a post . 而不是转移控制,做一个post

$.ajax({
    type: 'post',
    url: 'loginprivate.php'    //url where you want to post the stuff.
    data:{
        username: 'someUserName',
        password: 'xxxxxxxxxxxx'
    },
    success: function(res){
        //here you will get res as response from php page, either logged in or some error.
        //and if you're logged in successfully, redirect here now.
    }
});

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM