0

I have this code:

$(".user").click(function(){

    var s = $(this).attr('data-s');

    var r = $(this).attr('data-r');

    $.ajax({

        type: 'post',
        url: 'd/gcm.php',
        data: {s: s, r: r},
        success: function(cmd){
            $("#cet").show();
            $("#smd").attr("data-t",s);
            $("#smd").attr("data-tt",r);
            setInterval(function(){
                 $('#mcd').load('d/gcm.php');
            }, 100);


        }

    });

});

So on click post is requested. Inside gcm.php I have:

$s = mysqli_real_escape_string($con, $_POST['s']);
$r = mysqli_real_escape_string($con, $_POST['r']);
if($s!="" && $s!=NULL && $r!="" && $r!=NULL){ DO SOMETHING }else echo "Empty";

Now When I do this it works:

success: function(cmd){ .... and change interval with : $('#mcd').html(cmd); } Also when I try to add code above to interval nothing happens, script do not work. So I am wondering why I get message "Empty"; When it is after post request and post success?

Henders
  • 1,195
  • 1
  • 21
  • 27
  • fetching a page every 100 milliseconds? Why? Also GET requests cache, are you setting no-cache headers? – epascarello May 16 '16 at 11:16
  • Is that ALL that is in the `d/gcm.php` script? – RiggsFolly May 16 '16 at 11:18
  • console.log(cmd) and show what is the result of request – siddhesh May 16 '16 at 11:19
  • @RiggsFolly I am not talking about the Ajax call, load() is a GET. – epascarello May 16 '16 at 11:20
  • You do realise that doing a `$('#mcd').load('d/gcm.php');` every 100 milli seconds is going to TRASH your server so it does little else. Specially if 20 people are on your website are the same time – RiggsFolly May 16 '16 at 11:22
  • @epascarello Beg pardon, missed that first time round – RiggsFolly May 16 '16 at 11:24
  • @siddhesh I get what I want by cmd but I want to reload it because cmd is database output for messages between those two users. s is first user and r is second user. I am sending their info to other page to check does some messages exist between them. If they do i echo them and by jquery show in page with $("#somediv").html(cmd); But problem is how to reload that div every time i got message or i send message... – LittleProgrammer96 May 16 '16 at 11:24
  • Right: Your `.load` is not passing any parameters. So I would assume that `d/gcm.php` is crashing or at least getting into problems – RiggsFolly May 16 '16 at 11:26
  • This is far from the best way to write a chat room – RiggsFolly May 16 '16 at 11:27
  • @RiggsFolly I aksed like 20 times here what is best way to reload something without refresh page. They said setinterval is only option.. I am new with js so I really do not know other way. It is easy to load when I send message. But how to load when somebody else send to you... – LittleProgrammer96 May 16 '16 at 11:27
  • Maybe you didnt ask the question correctly, have a look at [WAMP Protocol](http://wamp-proto.org/) – RiggsFolly May 16 '16 at 11:29
  • If you add parameters to a `.load` it will also convert the call to a POST from the default which is GET which would also have been messing up your PHP script. Am I right here @epascarello – RiggsFolly May 16 '16 at 11:35
  • `load()` is `$.get()` under the covers. http://stackoverflow.com/questions/168963/stop-jquery-load-response-from-being-cached – epascarello May 16 '16 at 11:41
  • can you tell me what is the "mcd" is it a div?? – siddhesh May 16 '16 at 11:54
  • @RiggsFolly is it better to load every time when like if I output messages for 2 users and their ids are 1,4,5,6,9,13. And save last message with id 13. And make another ajax call where if I get new row with higher id num to load div. and then save highest id again. or number of outputed rows. – LittleProgrammer96 May 16 '16 at 12:01
  • @epascarello The `.load` method is GET by default, but from the [jQuery Manual](http://api.jquery.com/load/) `Request Method : The POST method is used if data is provided as an object; otherwise, GET is assumed.` – RiggsFolly May 16 '16 at 12:11

2 Answers2

0

.load() requests are GET requests. So you might change to jQuery.post() instead:

//setInterval(function(){
    $.post('d/gcm.php', {s: s, r: r}, function(d){
        $('#mcd').html(d);
    });
//}, 100);

and i don't think you should have a setInterval push here.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • 1
    Actually if you add parameters to a `.load` it is converted into a `type: POST'` automatically. Or so I just read in the jQuery documentation! Am I right here @epascarello – RiggsFolly May 16 '16 at 11:31
  • I did this code before, I get what I want but when I recive new data in mysql base I want to output it without reloading a page... I asked and people said set interval is only option for that. Can you tell me how to get info without reloading a page and without interval if it is possible... – LittleProgrammer96 May 16 '16 at 11:35
  • @LittleProgrammer96 then that's fine to use setInteval. Although Oberservers or polling would be other choice. – Jai May 16 '16 at 11:37
  • But Riggs said if I have many people it can crash my server... Is there any other way different from interval to load data? @Jai – LittleProgrammer96 May 16 '16 at 11:38
  • @Jai what do you think by saying polling, how? – LittleProgrammer96 May 16 '16 at 11:51
0

try this code use this to load after ajax success response

   $('.user').on("click",function(e){ 
      e.preventDefault(); // cancel click
      var page = $(this).attr('data-tt');   or 'data-t'
      $('#mcd').load(page);
   });
siddhesh
  • 563
  • 1
  • 9
  • 15
  • this is not what i need – LittleProgrammer96 May 16 '16 at 12:02
  • $.ajax({ type: 'post', url: 'd/gcm.php', data: {s: s, r: r}, success: function(cmd){ $("#cet").show(); $("#smd").attr("data-t",s); $("#smd").attr("data-tt",r); setInterval(function(){ var divreload= $(this).attr('data-tt'); or 'data-t' $('#mcd').load(divreload); }, 100); } }); – siddhesh May 16 '16 at 12:04