Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何基于jquery ajax响应发出警报_Php_Jquery_Ajax_Codeigniter - Fatal编程技术网

Php 如何基于jquery ajax响应发出警报

Php 如何基于jquery ajax响应发出警报,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,在php页面中,我有一个ajax jquery代码,它调用我的php控制器来执行特定的操作,当操作返回success时,我正在刷新特定的div 我的疑问是,当我的控制器响应到我的jquery函数时,我想执行其他操作,例如,如果用户未登录,那么我想发出类似警报(“请登录以继续”)的警报 我已经尝试了下面的代码,但它根本不起作用。请帮忙 <script type="text/javascript"> $('.voteup').click(functi

在php页面中,我有一个ajax jquery代码,它调用我的php控制器来执行特定的操作,当操作返回success时,我正在刷新特定的div

我的疑问是,当我的控制器响应到我的jquery函数时,我想执行其他操作,例如,如果用户未登录,那么我想发出类似
警报(“请登录以继续”)的警报

我已经尝试了下面的代码,但它根本不起作用。请帮忙

  <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>

你可以在整理空间后试试

  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);}
                }
首先需要将响应存储在主体内部。然后你可以检查一下。
$('.voteup')。单击(函数(e)
{  
//单击导航按钮链接时防止默认操作
e、 预防默认值();
变量$this=$(this),
id=$this.data('id'))
e、 预防默认值();
//用于检索HTML视图而不刷新页面的ajax查询。
$.ajax({
键入:“POST”,
url:“”,
数据:{id:id},
数据类型:“html”,
成功:函数(html)
{ 
$('#responseStore').html(html);
if($('#responseStore').text()=='未登录')
{ 
提醒(“您需要登录才能投票。”);
}
如果($('#responseStore')。text()=='own_question')
{ 
警惕(“你不能为你自己的问题投票”);
}
else{//当成功刷新div时
$('#questvotediv').html(html);}
}
});
返回false;
});

可能是PHP文档中有一些空格在echo命令之前或之后输出。如果是这样的话,你的比较可能会很混乱。
因此,我通常更喜欢使用INT,因为在您的情况下,您可以将PHP的返回值设置为0表示未登录,1表示自己的问题,然后在JS比较中使用parseFloat()函数并比较INT的

是否有响应来自
PHP
?是否尝试过console.log(html)看看你得到了什么样的回应?你确定你的控制器没有将一些布局信息与你的回声一起发送吗?@YogeshSuthar是的,正在发送。。如果我给出
alert(html)
那么它会显示基于控制器代码的结果,结果和初始状态之间有很大的空间,这就是为什么它不会执行
if(html==“own_-question”)
因为输出是“own_-question”,如果
alert(html),你会得到什么
成功function@dreamCoder如果我保持
警报(html),我将获得未登录的
。我想这就是我无法比较的原因。有什么东西可以让我修剪那些长的空间吗?我必须刷新我的分区。请再读一遍我的问题。我需要从我上面的jquery相关的解决方案尝试在成功中警告html数据:函数(html){html=$.trim(html);警报(html);}然后尝试完成这项工作,它是标准的吗?如果这解决了空白问题,我希望看到文档说明原因。非常奇怪,因为这是一个有趣的解决方案。当然它会工作,因为您正在匹配字符串,所以不存在空白问题。
  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);}
                }
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>