dongpo1203 2015-05-16 02:06
浏览 39

PHP重定向上的PHP AJAX / JQuery Bootstrap 3模态关闭

I am fairly new to PHP and AJAX/JQuery, but I do have a background in Class ASP VBScript.

I have 2 pages: FORM page and PROCESS page.

FORM PAGE:

  • I have a Bootstrap 3 modal login window.
  • Using AJAX to send form data to PROCESS PAGE and a "#Result" div for the reply
  • Upon successful credentials, I would like to redirect to a "Success" page, but instead, it just places the "Success" page's text in the "#Result" div inside the modal.
  • Upon successful credentials, I would like the login modal CLOSE, and then redirect to the "Success" page.

How can I do that? My code is below.

AJAX CODE

<HEAD>
    <script>

    $(document).ready(function() {

    $("#sub").click(function() {

    var user_name = $("#name").val();
    var user_email = $("#email").val();
    var user_pass = $("#pass").val();


    $.post("testloginprocess3.php",{name:user_name,email:user_email,pass:user_pass},function(data){

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

    });

    });

    });

    </script>
</HEAD>

FORM CODE

<form id="basicBootstrapForm" class="form-horizontal">

    <div class="form-group">
        <label class="col-xs-3 control-label">Name</label>
        <div class="col-xs-5">
            <input type="text" class="form-control" name="name" id="name" placeholder="Enter name" />
        </div>
    </div>


    <div class="form-group">
        <label class="col-xs-3 control-label">Email address</label>
        <div class="col-xs-5">
            <input type="email" class="form-control" name="email" id="email" placeholder="Enter email" required />
        </div>
    </div>

    <div class="form-group">
        <label class="col-xs-3 control-label">Password</label>
        <div class="col-xs-5">
            <input type="password" class="form-control" name="pass" id="pass" placeholder="Enter password" />
        </div>
    </div>

</form>

        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="submit" class="btn btn-primary" name="signup" value="Sign up" id="sub">Submit</button>

          <div id="result"></div>
        </div>

PROCESS PAGE:

PHP CODE

<?php


$con = mysqli_connect("localhost","root","","dbname");

$name = $_POST['name'];
$email = $_POST['email'];
$pass = $_POST['pass'];

$sel = "select * FROM USERS WHERE user_email = '$email' AND user_pass = '$pass'";

$run = mysqli_query($con,$sel);

$check_email = mysqli_num_rows($run);

if($check_email==1){

    Header("Location: testloginsuccess.php");

exit();
}else {

echo "<h4>Not Successful!</h4>";
}


?>
  • 写回答

1条回答 默认 最新

  • douqingji3026 2015-05-16 02:16
    关注

    $.post will not follow the redirection you're attempting to do in your PHP code (You can read more about this here). One way you can do it, is return a success or fail string (or even 1/0), instead of trying to redirect from within the php code, and then use this:

    $.post("testloginprocess3.php",{name:user_name,email:user_email,pass:user_pass},function(data){
    if ( data == 'success' ) {
        //Way #1, redirect the page:
        document.location.href = '/successpage';
        //Way #2, get the success page, and dump it into the $('#result');
        $.get('/successpage', {
           success: function(page) {
              $("#result").html(page);
           }
        }
    } else {
        //Login failed, do something here
    }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!