Welcome Guest, Not a member yet? Register   Sign In
[Solved] Refreshing Just A Div With Codeigniter
#1

(This post was last modified: 10-12-2016, 05:05 AM by wolfgang1983.)

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>
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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).
Reply
#3

(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?
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

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)
A good decision is based on knowledge and not on numbers. - Plato

Reply
#5

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.
Reply
#6

(This post was last modified: 10-12-2016, 05:12 AM by wolfgang1983.)

Solution now found http://stackoverflow.com/questions/39997...6#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>
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB