我尝试在成功验证表单后发出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
{
}
}
答案 0 :(得分:1)
我的ajax添加此
data:{
username: 'root',
password: '',
submit: true,// add this line as you are checking in php file.
},