【问题标题】:Reload a page after successfully submit a form using ajax使用ajax成功提交表单后重新加载页面
【发布时间】:2015-07-03 08:13:35
【问题描述】:

我目前正在使用 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 脚本是

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

现在如果我们收到“您已成功登录”消息刷新当前页面,否则只显示错误消息并且不刷新页面。

我可以选择添加

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

但这仅适用于 chrome 而不是 Firefox。

【问题讨论】:

    标签: php ajax asynchronous


    【解决方案1】:

    你可以把条件放在循环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>
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

        【解决方案3】:
        <script>
            $(document).ready(function(){
                $("#submit").click(function(){
                    var emailnew = $("#email").val();
                    var  password = $("#password").val();
        
                    var dataString = '&email='+ email + '&password='+ password;
                    if(emailnew == null || password == null)
                    {
                        document.getElementById('error3').innerHTML="Please Fill All Fields";
                    }
                    else
                    {
                        $.ajax({
                            type: "POST",
                            url: "process.php",
                            data: dataString,
                            cache: false,
                            success: function(result)
                            {
                                if(result == "logged in true")
                                {
                                    window.setTimeout(function(){location.reload()},1000); //reload with time
                                    location.reload(); //reload without time
                                }
                                else
                                {
                                    document.getElementById('error3').innerHTML=result;
                                }
        
        
                            }
        
                        });
                    }
                    return false;
                });
            });
        </script>
        

        【讨论】:

          【解决方案4】:

          您的 process.php 应该返回一个结果,如成功 (true) 和不成功 (false) 标志。或用户ID,例如,如果它成功,则其他一些标志,如果它不是。您不应该在您的 proccess.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 中设置会话。

          【讨论】:

            【解决方案5】:

            window.location.reload() 用于使用 javascript 重新加载页面。 或者你可以使用window.location.href=window.location.href;

            【讨论】:

              猜你喜欢
              • 2020-06-27
              • 2012-04-18
              • 2011-12-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-09
              • 1970-01-01
              相关资源
              最近更新 更多