0

I have a page for live scores results for each match the page have two sections the up section which have the live result and match information and the down section which has comments area. when i make a comment and click submit the page refresh and go up to the information section i want to add the comment and refresh the page and then go down to the place of the comment (not going up). my code of the comment box is:

$("#section_bar").after('<textarea rows="4" cols="50"  id="" class="comment_area" name="reply_area" ></textarea><a class="comment button small black" href="#" id="reply_'+match_id+'"><span>submit</span></a>');

            $("#reply_"+match_id).live('click',function(){
            text_area_value = $('textarea').val();
                $.ajax({
                    type: "POST",
                    url: 'comment_proxy.php',
                    data:{id:match_id, user_id:user_id, reply:text_area_value, date:today1, time:time},
                    dataType: "json",
                    success: function(data)
                    {
                        location.reload();          
                    },
                    error : function(XMLHttpRequest, textStatus, errorThrown) {
                        /*alert(XMLHttpRequest);
                        alert(textStatus);
                        alert(errorThrown);
                        alert(XMLHttpRequest.responseText);*/
                    }
                });
            });

My question is : IS there any line of code that I can add it after location.reload() to force the page to submit and refresh and then stay at the same point or return to the same point that we was in before the submitting ????

Basel
  • 359
  • 3
  • 16
  • 2
    Why are you refreshing the page at all? Most of the point of Ajax is to avoid page reloads so this kind of thing doesn't happen... – Matt Gibson Mar 02 '14 at 11:36
  • if i did not refresh that it would not submit the comment??? did u think that my ajax format is wrong ? – Basel Mar 02 '14 at 11:41
  • If you want to use your approach, take a look at using a hash: http://stackoverflow.com/questions/7819547/how-to-implement-jump-to-a-name-in-post-form – Asons Mar 02 '14 at 11:44
  • 1
    @Basel success function will call only when your data is submitted successfully..!! so u can follow different approach rather to refresh page. such as appending latest comment to the previous all comments if any.. – Aman Gupta Mar 02 '14 at 11:45

1 Answers1

0

Instead of reloading your page, location.reload();, and without knowing how your data result is formatted, you can add the result from your post like this:

Using javascript (as I am no jQuery developer)

success: function(data)
{
    // add the "data" to your div
    document.getElementById("div_with_posts_id").innerHTML += data;

    // maybe clear/reset the form here ?

},
Asons
  • 84,923
  • 12
  • 110
  • 165