【问题标题】:Ajax Redirect on new page and after display success message在新页面上和显示成功消息后的 Ajax 重定向
【发布时间】:2016-07-16 11:20:25
【问题描述】:

我正在使用 ajax 和 php。用户使用 Ajax 成功添加后,我想在另一个页面上显示消息。我已经尝试了很多。当第一次在弹出窗口上显示“成功”消息并在另一个页面上重定向时,它正在工作。但我想先重定向到另一个页面,然后显示消息。

用户添加页面:

<form action="<?php echo $action_link; ?>" method="post" id="form_user_profile" class="form-horizontal" novalidate="novalidate">

   <input type="hidden" id="id" name="id" value="<?php echo $id; ?>">

   <div class="form-body">
     <div class="form-group">
       <label class="control-label col-md-3">First Name
         <span class="required" aria-required="true"> * </span>
       </label>
       <div class="col-md-4">
         <div class="input-icon right">
           <i class="fa"></i>
           <input type="text" class="form-control" name="fname" value="<?php echo $user_db[0]['fname']; ?>"> 
         </div>
       </div>
     </div>
     <div class="form-actions">
       <div class="row">
         <div class="col-md-offset-3 col-md-9">
           <input type="submit" class="btn green" name="submit" value="<?php echo $addupdate_msg; ?>">
         </div>
       </div>
     </div>
</form>

验证中的 Ajax

$("#form_user_profile").validate({
        rules: {
            fname: {
                required: true
            }
        },
        messages: {
            fname: "Please enter first name"
        },
        submitHandler: function(form) {
            $.ajax({
                url: form.action,
                type: form.method,
                data: $(form).serialize(),
                success: function(response) {
                    if(response == 1){
                        bootbox.alert("User has been added successfully.", function() {
                            window.location.href= "<?php echo $user_list; ?>";
                        });
                    }
                }
            });
        }
});

动作

if (isset($_REQUEST['submit']) && $_REQUEST['submit'] == "Add") {

    $fname = mysqli_real_escape_string($obj->CONN, $_REQUEST['fname']);    

    $user->setfname($fname);
    $insert = $user->insert();

    ob_get_clean();

    if($insert){
        echo '1';
        $_SESSION['msg'] = "New user has been added successfully.";
    }else{
        echo '0';
    }

    exit;
}

【问题讨论】:

  • 给 1 加上单引号 if(response == '1'){ }
  • 这应该没什么区别,因为他使用的是松散的== 比较。
  • @jothi 它不像 if(response == '1'){ } 那样工作。实际上我想在另一个页面上显示成功消息。使用这个,在另一个页面上重定向,没关系。但不显示任何成功消息。
  • @NeelThakkar;你想在页面 another_page.php 中显示 $_SESSION['msg'] 吗?您是否在页面顶部操作开始会话?
  • @NeelThakkar,我认为这是会话的问题,输入你的 if($insert){ $_SESSION[''msg]= "just test";回声 $_SESSION[''msg]; }。在 ajax 中,成功:function(response) { alert(esponse); ... } 你得到了什么?

标签: php html ajax jquery-validate


【解决方案1】:

由于以下代码,在用户被重定向之前会出现成功弹出窗口:

if(response == 1){
    bootbox.alert("User has been added successfully.", function() {
        window.location.href= "<?php echo $user_list; ?>";
    });
}

如果您希望在新页面上显示一条消息,只需重定向用户

if(response == 1)window.location.href= "<?php echo $user_list; ?>";

在成功后发送客户的新页面上,显示类似

的消息
$(function() {
    bootbox.alert(<?= $_SESSION['msg'] ?>);
});

【讨论】:

  • 我已经在另一个页面上设置了会话,但仍然没有显示任何来自会话的成功消息。
【解决方案2】:

您可以检查您要引导到的页面上是否设置了$_SESSION['msg'],如果设置了则alert

jquery:

if(response == 1){window.location.href= "<?php echo $user_list ?>";}

然后,$user_list 页面:

if(isset($_SESSION['msg'])){
  echo '$(document).ready(function(){
            bootbox.alert('.$_SESSION['msg'].');
        })';
}

注意,我使用单引号是为了让 PHP 不认为 $ 是一个变量。

【讨论】:

  • 非常感谢。
猜你喜欢
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多