Php 使用ajax成功提交表单后重新加载页面
我目前正在使用ajax制作一个登录表单Php 使用ajax成功提交表单后重新加载页面,php,ajax,asynchronous,Php,Ajax,Asynchronous,我目前正在使用ajax制作一个登录表单 <div id='error3'></div> <input type='email' required name='email' id='email' placeholder='Email'> <input type='password' required name='password' id='password' placeholder='password'> <input type='submit
<div id='error3'></div>
<input type='email' required name='email' id='email' placeholder='Email'>
<input type='password' required name='password' id='password' placeholder='password'>
<input type='submit' name='submit99' id='submit' value='login'>
我的java脚本是
<script>
$(document).ready(function(){
$("#submit").click(function(){
var emailnew = $("#email").val();
var password = $("#password").val();
var dataString = '&email='+ email + '&password='+ password;
if(emailnew==''|| password='')
{
document.getElementById('error3').innerHTML="Please Fill All Fields";
}
else
{
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function(result){
document.getElementById('error3').innerHTML=result;
}
});
}
return false;
});
});
<?php
if(isset($_POST['email']))
{
if (filter_var($email99, FILTER_VALIDATE_EMAIL))
{
$con=mysqli_connect('localhost','root','password','database');
$query="SELECT * FROM TABLENAME WHERE USER='' AND PASSWORD=''";
$result=mysqli_query($con,$query);
if(mysqli_num_rows($result)!=0)
{
echo "You are successfully logged in";
login user
start a cookie or session
}
else
{
echo "You are bot a valid user";
}
}
else
echo "not a valid email";
}
?>
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var emailnew=$(“#email”).val();
var password=$(“#password”).val();
var dataString='&email='+email+'&password='+password;
如果(emailnew=''| |密码='')
{
document.getElementById('error3').innerHTML=“请填写所有字段”;
}
其他的
{
$.ajax({
类型:“POST”,
url:“process.php”,
数据:dataString,
cache:false,
成功:功能(结果){
document.getElementById('error3')。innerHTML=result;
}
});
}
返回false;
});
});
我的process.php脚本是
<script>
$(document).ready(function(){
$("#submit").click(function(){
var emailnew = $("#email").val();
var password = $("#password").val();
var dataString = '&email='+ email + '&password='+ password;
if(emailnew==''|| password='')
{
document.getElementById('error3').innerHTML="Please Fill All Fields";
}
else
{
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function(result){
document.getElementById('error3').innerHTML=result;
}
});
}
return false;
});
});
<?php
if(isset($_POST['email']))
{
if (filter_var($email99, FILTER_VALIDATE_EMAIL))
{
$con=mysqli_connect('localhost','root','password','database');
$query="SELECT * FROM TABLENAME WHERE USER='' AND PASSWORD=''";
$result=mysqli_query($con,$query);
if(mysqli_num_rows($result)!=0)
{
echo "You are successfully logged in";
login user
start a cookie or session
}
else
{
echo "You are bot a valid user";
}
}
else
echo "not a valid email";
}
?>
试试这个:
<script>
$(document).ready(function () {
$("#submit").click(function () {
var emailnew = $("#email").val();
var password = $("#password").val();
var dataString = '&email=' + email + '&password=' + password;
if (emailnew == '' || password = '') {
document.getElementById('error3').innerHTML = "Please Fill All Fields";
}
else {
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function (result) {
document.getElementById('error3').innerHTML = result;
if (result == "You are bot a valid user") {
window.location.href = "mypage.html"
}
}
});
}
return false;
});
});
</script>
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var emailnew=$(“#email”).val();
var password=$(“#password”).val();
var dataString='&email='+email+'&password='+password;
如果(emailnew=''| |密码=''){
document.getElementById('error3').innerHTML=“请填写所有字段”;
}
否则{
$.ajax({
类型:“POST”,
url:“process.php”,
数据:dataString,
cache:false,
成功:功能(结果){
document.getElementById('error3')。innerHTML=result;
如果(结果=“您是有效用户”){
window.location.href=“mypage.html”
}
}
});
}
返回false;
});
});
您可以将条件放入循环成功:函数(结果){
<script>
$(document).ready(function () {
$("#submit").click(function () {
var emailnew = $("#email").val();
var password = $("#password").val();
var dataString = '&email=' + email + '&password=' + password;
if (emailnew == '' || password = '') {
document.getElementById('error3').innerHTML = "Please Fill All Fields";
} else {
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function (result) {
if (result == "You are successfully logged in") {
document.location.href = 'login.htm';
} else {
document.getElementById('error3').innerHTML = result;
}
}
});
}
return false;
});
});
</script>
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var emailnew=$(“#email”).val();
var password=$(“#password”).val();
var dataString='&email='+email+'&password='+password;
如果(emailnew=''| |密码=''){
document.getElementById('error3').innerHTML=“请填写所有字段”;
}否则{
$.ajax({
类型:“POST”,
url:“process.php”,
数据:dataString,
cache:false,
成功:功能(结果){
如果(结果=“您已成功登录”){
document.location.href='login.htm';
}否则{
document.getElementById('error3')。innerHTML=result;
}
}
});
}
返回false;
});
});
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var emailnew=$(“#email”).val();
var password=$(“#password”).val();
var dataString='&email='+email+'&password='+password;
如果(emailnew==null | |密码==null)
{
document.getElementById('error3').innerHTML=“请填写所有字段”;
}
其他的
{
$.ajax({
类型:“POST”,
url:“process.php”,
数据:dataString,
cache:false,
成功:功能(结果)
{
如果(结果=“登录为真”)
{
setTimeout(函数(){location.reload()},1000);//随时间重新加载
location.reload();//无时间重新加载
}
其他的
{
document.getElementById('error3')。innerHTML=result;
}
}
});
}
返回false;
});
});
您的process.php应该返回一个结果,如successfull(true)和successful(false)标志。或者如果成功返回userID,如果不成功则返回其他标志。您不应该回显process.php文件中的通知。然后,您的javascript代码中可以有类似的内容:
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
cache: false,
success: function(result){
if(result=='successful'){
showMessageAndredirectFunction();
//in this functionyou can show the message in your html page like the way you do for error and with a delay redirect the page with javascript
else
document.getElementById('error3').innerHTML="Bad usernaem or password";
}});
如果会话成功,您可以在process.php中设置会话。窗口。location.reload()
用于使用javascript重新加载页面。
或者您可以使用window.location.href=window.location.href;