如何在ajax“成功后”弹出:

时间:2015-08-22 03:52:07

标签: javascript php ajax

我有一个按钮,当我点击此按钮时,它将转到ajax。在suceess条件下,它返回“data”的三个值,即1,2,3。我想只在data = 1时弹出登录表单。(如果用户没有登录)。请帮帮我。

brew update
brew install leiningen

popup div

 <button data-toggle="modal" data-target="#xmpModal" class="btn green btn-success" onclick="get('<?echo $u_id;?>','<?echo $e_id;?>')">click</button>

AJAX

   <div class="modal fade" id="exmpModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                        <h3 class="modal-title" id="lineModalLabel">My Modal</h3>
                    </div>
                    <div class="modal-body">

                        <!-- content goes here -->
                        <form>
                          <div class="form-group">
                            <label for="exampleInputEmail1">Email address</label>
                            <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
                        </div>
                        <div class="form-group">
                            <label for="exampleInputPassword1">Password</label>
                            <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                        </div>

                      <button type="submit" class="btn btn-default">Submit</button>
                  </form>

              </div>
              <div class="modal-footer">
                <div class="btn-group btn-group-justified" role="group" aria-label="group button">
                    <div class="btn-group" role="group">
                        <button type="button" class="btn btn-default" data-dismiss="modal"  role="button">Close</button>
                    </div>
                    <div class="btn-group btn-delete hidden" role="group">
                        <button type="button" id="delImage" class="btn btn-default btn-hover-red" data-dismiss="modal"  role="button">Delete</button>
                    </div>
                    <div class="btn-group" role="group">
                        <button type="button" id="saveImage" class="btn btn-default btn-hover-green" data-action="save" role="button">Save</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

我在php控制器中的功能

  function get(u_id,e_id)
{
    $.ajax({

        url: "<?echo base_url()?>events/xyz",
        type: 'post',   // HTTP METHOD
        data: 
        {u_id:u_id,e_id:e_id  },


        success: function(data)
        {  

             //alert(data);  
            if(data==1) 
            {
                // add your code pls
            }                                    
           else if(data==2)

           {
                alert("hai");

           }
           else if(data==3)   
           {
             alert("hello"); 
           }        
        }

      });
  }

2 个答案:

答案 0 :(得分:1)

我假设你正确地获得1, 2, 3并且可以操纵它们,所以只需要显示modal

if($data == 2){
    $('#exmpModal').modal('show');
}

you can read more about Modal here

更多Specifically

答案 1 :(得分:0)

  

您需要从控制器返回json,如下所示:

我不确定你使用的是什么框架,但这个例子在返回json时是laravel框架。

return Response::json(array('response'=>'values')); 
  

在你的js成功函数中:

success: function(data)
        { 
            if(data.response==1) 
            {
                // add your code pls
            }                                    
           else if(data.response==2)

           {
                alert("hai");

           }
           else if(data.response==3)   
           {
             alert("hello"); 
           }        
        }