奇怪的锚标签行为

问题描述:

In my view im doing this :

<?php if($user_can_write) {?>
<a href=<?php echo base_url('backend_controllers/users/foo')?> style="text-decoration:none">
Add</a>
<?php } ?>

I pass variable $user_can_write from my controller , which is my implementation for access control, the strange part is whenever i refresh this page , ie this view , the anchor tag is executed , ie , the foo function in user controller is called and executed , every time !Basically the foo function increments the db by one row (adds row to the db), so this is really not practical for me that on every refresh the anchor tag executes

Now if i do this implementation in the view (alternate approach) :

<?php if($user_can_write) {?>
<a href='javascript:void(0);' onclick="on_click_method_for_anchor()" style="text-decoration:none">
Add</a>
<?php } ?>

and further in my javascript i do this :

function on_click_method_for_anchor(){
$.ajax({
            url: '<?php echo base_url('backend_controllers/user/foo'); ?>',            
            type: 'POST',
            success: function(result){                
                $('.my_span_tag_class').html(result)
            }
        });

this seems to work perfect , and even if i reload the page , the foo is not called , can someone please tell me why is the anchor tag behaving like this ? am i missing out something obvious ?

在我看来我这样做: p>

 &lt;?  php if($ user_can_write){?&gt; 
&lt; a href =&lt;?php echo base_url('backend_controllers / users / foo')?&gt;  style =“text-decoration:none”&gt; 
Add&lt; / a&gt; 
&lt;?php}?&gt; 
  code>  pre> 
 
 

我从我传递变量$ user_can_write 控制器,这是我的访问控制实现,奇怪的部分 strong>是每当我刷新这个页面,即这个视图,执行锚标记,即调用和执行用户控制器中的foo函数, 每次!基本上foo函数将db增加一行(向db添加行),因此对于我来说,每次刷新执行锚标记都是不实际的 p>

现在,如果 我在视图中执行此实现(替代方法): p>

 &lt;?php if($ user_can_write){?&gt; 
&lt; a href ='javascript:void(  0);”  onclick =“on_click_method_for_anchor()”style =“text-decoration:none”&gt; 
Add&lt; / a&gt; 
&lt;?php}?&gt; 
  code>  pre> 
 
 

进一步在我的javascript中我这样做: p>

  function on_click_method_for_anchor(){
 $ .ajax({
 url:'&lt;?php echo base_url('backend_controllers /  user / foo');?&gt;',
 type:'POST',
 success:function(result){
 $('。my_span_tag_class')。html(result)
} 
});  
  code>  pre> 
 
 

这似乎工作得很完美,即使我重新加载页面,也没有调用foo,有人可以告诉我为什么锚标签 表现得像这样 strong>? 我错过了一些明显的东西吗? p> div>

I think you miss the " " part just after href properties of anchor tag.

<?php if($user_can_write) {?>
<a href="<?php echo base_url('backend_controllers/users/foo')?>" style="text-decoration:none">
Add</a>
<?php } ?>

Try this. I think it will be ok