CodeIgniter Forums
[Solved] Refreshing Just A Div With Codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Solved] Refreshing Just A Div With Codeigniter (/showthread.php?tid=66343)



[Solved] Refreshing Just A Div With Codeigniter - wolfgang1983 - 10-12-2016

On my script I am able to submit my query form and then it refreshes the div OK

How ever it only seems to refresh once if I click it multiple times will not refresh

Any suggestions on what best way is?




Code:
if (response.success == true) {
   $('.rep').load(window.location.href + ' .rep');
}




Code:
<script type="text/javascript">
$(document).ready(function() {
    $('#vote-up-icon').on('click', function(e) {
        e.preventDefault();
        $.ajax({
            url: "<?php echo base_url('thread/voteup/');?><?php echo $thread_id;?>",
            type: 'post',
            dataType: 'json',
            data: {
                voteup: $(this).parent().find('#vote_up').val()
            },
            success: function(response){
                if (response.success == true) {
                    $('.rep').load(window.location.href + ' .rep');
                }
            }  
        });
    });
});      
</script>



RE: Refreshing Just A Div With Codeigniter - PaulD - 10-12-2016

If you remove the html then the click function associated with it is also lost. Do you not just have to reapply the click function? (Assuming here you are applying the click function after page load).


RE: Refreshing Just A Div With Codeigniter - wolfgang1983 - 10-12-2016

(10-12-2016, 03:46 AM)PaulD Wrote: If you remove the html then the click function associated with it is also lost. Do you not just have to reapply the click function? (Assuming here you are applying the click function after page load).

Unsure what you mean.

It submits the form then if success it should refresh the div each time I click on button. How ever it only refreshes div once for some reason?


RE: Refreshing Just A Div With Codeigniter - salain - 10-12-2016

It seams you are doing 2 request to the server the ajax post and load in the success handler.
Would it not be better to return the updated content of the div with the ajax response

Some thing like this:


Code:
<script type="text/javascript">
$(document).ready(function() {
   $('#vote-up-icon').on('click', function(e) {
       e.preventDefault();
       $.ajax({
           url: "<?php echo base_url('thread/voteup/');?><?php echo $thread_id;?>",
           type: 'post',
           dataType: 'json',
           data: {
               voteup: $(this).parent().find('#vote_up').val()
           },
           success: function(response){
                resp = $.parseJSON(response);
               if (resp.success == true) {
                   $('.rep').html(resp.div_content);
               }
           }  
       });
   });
});      
</script>


Your php method echo a json_encode($result_array)


RE: Refreshing Just A Div With Codeigniter - Waschi - 10-12-2016

You should debug the script. So you can have clear, where it stops working.
Log Click Event, Success and Error Response, Data Value...
There could be a lot of issues, that are could cause it.


RE: Refreshing Just A Div With Codeigniter - wolfgang1983 - 10-12-2016

Solution now found http://stackoverflow.com/questions/39997726/when-click-on-div-multiple-times-will-not-refresh-only-refreshes-once/39998126#39998126

Have had to add

Code:
   $('#rep').on('click', '#vote-up-icon', function(e) {




Code:
<script type="text/javascript">
$(document).ready(function() {
   $('#rep').on('click', '#vote-up-icon', function(e) {
       e.preventDefault();
       $.ajax({
           url: "<?php echo base_url('thread/voteup/');?><?php echo $thread_id;?>",
           type: 'post',
           dataType: 'json',
           data: {
               voteup: $(this).parent().find('#vote_up').val()
           },
           success: function(response){
               if (response.success == true) {
                   $('#rep').load(window.location.href + ' #rep');
               }
           }  
       });
   });
});    
</script>