简体   繁体   English

通过AJAX提交后如何显示模式形式

[英]How to display modal form after submitting through AJAX

I Have a login in form inside a modal. 我在模态表单中有一个登录名。 After submitting the form i am getting success or failure message..let's say failure message. 提交表单后,我收到成功或失败的消息。表示失败消息。 But when i am reopening the modal same message is appearing instead of log in form. 但是当我重新打开模态时,相同的消息出现而不是登录表单。 After refreshing the page only modal is showing the log in form. 刷新页面后,只有模态显示登录表单。

Here is my code....please help me how can i fix it so it will show the log in form when i reopen the modal after submission. 这是我的代码....请帮助我如何解决它,以便在我提交后重新打开模态时显示登录表单。

    <!--  Sign in feature starts here --> 
<div id="sign_in" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Sign In</h4>
      </div>
      <div class="modal-body">
<div class="panel-body" id="form">
 <form class="form-horizontal" role="form" method="post" id="log-in" >
  <fieldset>
                                <div class="form-group">
                               <input class="form-control" id="username" placeholder=" Username or Mobile No." type="text" autofocus  AutoComplete=off required>
                                </div>
                                <div class="form-group">
                               <input class="form-control" id="password" placeholder=" Enter Password" type="password" autofocus  AutoComplete=off required>
                                </div>
                                 <!-- Change this to a button or input when using this as a form -->
                                <button class="btn btn-lg btn-success btn-block" type="submit">Sign In</button>
                            </fieldset>
  </form> </div>
</div>
          </div> 
</div></div>
 <!--  Sign In feature ends here -->
<script type="text/javascript">
$(document).ready(function()
{   
    $(document).on('submit', '#log-in', function()
    {
        var data = $(this).serialize();
        $.ajax({
        type : 'POST',
        url  : '<?php echo site_url('verifylogin');?>',
        data : data,
        success :  function(data)
                   {                        
                        $("#log-in").fadeOut(500).hide(function()
                        {
                            $("#form").fadeIn(500).show(function()
                            {
                                $("#form").html(data);
                                if(data != null && data == "success"){ //redirect...
                        window.location.replace('home.php');
                }
                            });
                        });

                   }
        });

        return false;
    });
});
</script>

The problem is at $("#form").html(data); 问题出在$("#form").html(data); Why are you removing the form HTML and replacing it with the data you are receiving ? 为什么要删除HTML表单并将其替换为所接收的数据?

When are you putting back the form HTML in the modal? 你什么时候在表格中放回表格HTML? It is working after refresh because the page is getting loaded again. 它在刷新后工作,因为页面再次加载。

In the previous case you have updated the DOM, removed the form HTML and replaced it with data that you are receiving. 在前一种情况下,您已更新DOM,删除了表单HTML并将其替换为您收到的数据。 So the next time you open the modal you will get to see the same data. 因此,下次打开模态时,您将看到相同的数据。

try to change this part 尝试改变这部分

 $("#form").html(data);
 if(data != null && data == "success")
 { //redirect...
       window.location.replace('home.php');
 }

TO

 $("#form").html(data);

ie REMOVE THIS PART 即删除此部分

 if(data != null && data == "success")
 { //redirect...
       window.location.replace('home.php');
 }

Also add this div somewhere in your code; 还要将该div添加到代码中的某个位置; This is the div that returns the result from your PHP code. 这是从您的PHP代码返回结果的div。

<div id="form"></div>

If redirecting is your success. 如果重定向是您的成功。 Then add the redirecting code in your success definition in the PHP code ie 然后在PHP代码的成功定义中添加重定向代码,即

<?php

echo 'please wait! redirecting...';
echo "<script>window.location.href='home.php';</script>";

?>

Ajax does not understand what your PHP means by success. Ajax无法理解PHP对成功的意义。 Failure might be success to another person and failure to another person. 失败可能是另一个人的成功和另一个人的失败。

In your question, if someone enters wrong username or password. 在您的问题中,如果有人输入了错误的用户名或密码。 it is likely to redirect Because that is also a success. 它很可能重定向,因为这也是成功的。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM