jQuery验证后的ajax请求 - javascript

在成功进行表单验证后,我尝试发出ajax请求。如果我在,之后删除url: 'loginprivate.php',则php代码有效,但验证无效。如果我添加,,则验证有效,但php代码无效。成功的ajax请求后的重定向仍然无法正常工作。我怎样才能使之工作,也许使用$(form).ajaxSubmit();,是的时候我应该在哪里添加这一行?

 $(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/localcorps/main.php";
    }
});
                return false; // for demo
            }
        });
    });

我的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
            {

            }
    }

参考方案

我你的ajax添加这个

data:{
    username: 'root',
    password: '',
    submit: true,// add this line as you are checking in php file.
},

如果我得到url(''),我该如何使用另一个URL - javascript

我是新手,正在写这篇文章,但是如果源上没有图像,那么我只有空白。有人可以告诉我,如果我正在获取背景图像,如何获取/images/no-image.jpg:url();这是我的代码:<div class="uk-clearfix uk-position-relative"> <div class="recipeb…

使用JS和PHP更改弹出窗口背景图像 - javascript

我有一个JS函数:function zoom(now) { document.getElementById("popup").style.display = "block"; document.getElementById("photos").style.backgroundImage = …

执行onclick时获得意外令牌 - javascript

我正在使用onclick事件从PHP调用JS函数。这是我的代码:我在一个函数中,因此我需要通过PHP来完成它,因为然后我会返回:$html = '<input type="checkbox" checked value="1" id="setGetSku" name="se…

PHP Javascript更改浏览器后退按钮行为Laravel - javascript

我知道有各种各样的线程要求几乎相同的要求,但似乎没有一个真正满足我的需求。在我的网站上,我实现了搜索表单。一个简单的表单,其中包含一个名为searchQuery的输入字段和一个提交按钮。表单通过POST方法发送。我正在使用Laravel btw。。然后将搜索结果从控制器加载到视图中。这些在表中显示。现在来了有趣的部分:找到的元素是可单击的,并且您进入有关该元…

Javascript-从当前网址中删除查询字符串 - javascript

单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…