使用ajax成功提交表单后重新加载页面

编程入门 行业动态 更新时间:2024-10-27 22:18:55
本文介绍了使用ajax成功提交表单后重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用ajax进行登录

I am currently a making a log in form using 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' 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; }); });

我的process.php脚本是

and my process.php script is

<?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"; } ?>

现在,如果我们收到一条消息您已成功登录",则刷新当前页面,否则仅显示错误消息,而不刷新页面.

now if we recive a message "You are successfully logged in" refresh the current page otherwise show only error message and do not refresh the page.

我可以选择添加

<meta http-equiv="refresh" content="30">

但这只能在chrome中使用,而不能在Firefox中使用.

but this works only in chrome not in firefox.

推荐答案

您可以将条件放入循环 success:function(result){

You can put the condtion inside loop success: function(result){

<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>

更多推荐

使用ajax成功提交表单后重新加载页面

本文发布于:2023-11-24 22:34:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   加载   页面   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!