1

PHPページには、PHPコントローラーを呼び出して特定のアクションを実行するajax jqueryコードがあります。アクションが成功を返すと、特定のdivを更新しています。

私の疑問は、応答がコントローラーからjquery関数に送信されたときに、他のアクションを実行したいということです。ユーザーがログインしていない場合、次のようなアラートを出したいalert("Please login to continue");

以下のコードを試しましたが、まったく機能しません。助けてください

  <script type="text/javascript">         
        $('.voteup').click(function(e)
        {  
            // prevent the default action when a nav button link is clicked
            e.preventDefault(); 
            var $this = $(this),
            id = $this.data('id') 
            e.preventDefault(); 
            // ajax query to retrieve the HTML view without refreshing the page.
            $.ajax({
                type: 'POST',
                url:"<?php echo base_url('question/ajax_vote_up/'); ?>",
                data: {id: id},  
                dataType: 'html',
                success: function (html) 
                { 
                    if(html == 'not_logged_in')//This is not working, not giving alert 
                    { 
                        alert("You need to login to vote.");
                    }
                     if(html == 'own_question')//This is not working, not giving alert 
                    { 
                        alert("You can not vote for your own question");
                    }
                    else{ //when success refresh the div
                        $('#questvotediv').html(html);}
                }
            });
            return false;
        });
    </script>

私のコントローラーで

function ajax_vote_up() 
{  

  $answer_id = $this->input->post('id'); 

   if ($answer_id > 0 && $this->session->userdata('logged_in') && $this->askmodel->get_user_id_By_answer_id($answer_id)!=$this->session->userdata('userid')) 
   {  
     //Few business logic related to my requirement
       $this->load->view('users/pages/show_question_By_id/votediv', $data); 
  }
   else if($this->askmodel->get_user_id_By_answer_id($answer_id)==$this->session->userdata('userid'))
  {
       //when user try to vote for their own quetion
     echo "own_question";
  }
  else
    {//Not looged in
      echo "not_logged_in";
    }
} 
4

3 に答える 3

0

Yuはスペースをトリミングした後に試すことができます

  success: function (html) 
           { 
                html = $.trim(html);
                if(html == 'not_logged_in')//This is not working, not giving alert 
                { 
                    alert("You need to login to vote.");
                }
                if(html == 'own_question')//This is not working, not giving alert 
                { 
                    alert("You can not vote for your own question");
                }
                else{ //when success refresh the div
                        $('#questvotediv').html(html);}
                }
于 2013-04-11T10:40:40.023 に答える
0
You need to store response inside body first. Then You can check it.

<script type="text/javascript">         
    $('.voteup').click(function(e)
    {  
        // prevent the default action when a nav button link is clicked
        e.preventDefault(); 
        var $this = $(this),
        id = $this.data('id') 
        e.preventDefault(); 
        // ajax query to retrieve the HTML view without refreshing the page.
        $.ajax({
            type: 'POST',
            url:"<?php echo base_url('question/ajax_vote_up/'); ?>",
            data: {id: id},  
            dataType: 'html',
            success: function (html) 
            { 
                $('#responseStore').html(html);

                if($('#responseStore').text() == 'not_logged_in')
                { 
                    alert("You need to login to vote.");
                }
                 if($('#responseStore').text() == 'own_question')
                { 
                    alert("You can not vote for your own question");
                }
                else{ //when success refresh the div
                    $('#questvotediv').html(html);}
            }
        });
        return false;
    });
</script>
<div style="display:none;" id="responseStore"></div>
于 2013-04-11T10:41:59.177 に答える