0

I've got some data that will be used as part of an image gallery but I don't want to refresh the page between loading the data (there are interface items that I wish to stay visible). However when I submit the data to the same page via ajax JS registers a successful execution of the code (the alert box shows up) but PHP fails to harvest the data.

My JQuery looks like this

$(".gallery_thumbnail").click(function(){
  var id=$(this).attr("data-id");
  $.ajax({
    type: "GET",
    url: "test.php",
    data: {newid:id},
    dataType: 'text',
    success:function(result){
      // Test what is returned from the server
      alert(result);
    }
  });
});

And my PHP looks like this\

if(isset($_GET['newid'])){
  echo "success";
}
else{
  echo "fail";
}

I've seen similar questions and tried copy and pasting the answers but I can't seem to get this to work. I've also tried for the url parameter:

http://localhost/test.php and simply removing the url parameter altogther.
user1352096
  • 21
  • 1
  • 5
  • Are you saying `alert(result)` is showing you `if(isset($_GET['newid'])){...` ? – Robbert Apr 11 '14 at 15:50
  • If you are submitting a form, you should use POST instead of GET. If you are going to send an ajax request, you should check it at the server http://stackoverflow.com/questions/4301150/how-do-i-check-if-the-request-is-made-via-ajax-with-php. – Cheluis Apr 11 '14 at 15:56
  • all guys that gives answers here want to OP understand issue resolving instead copy-pasting and ask almost similar question through the week – vp_arth Apr 11 '14 at 15:57
  • alert(result) shows the entire page dom (HTML Code). I'm not actually submitting a form but if I use POST or GET the result fails. – user1352096 Apr 11 '14 at 16:00

2 Answers2

1

Check if the request is ajax, then do the ajax processing

// this code should go before any of the web page code
if (isset($_GET['ajax'])){
    if(isset($_GET['newid'])){
      echo "success";
    }
    else{
      echo "fail";
    }
    exit;
}

set a param to see if it is an ajax request

  $.ajax({
    type: "GET",
    url: "test.php",
    data: {newid:id, ajax: 'true'},
    dataType: 'text',
    success:function(result){
      // Test what is returned from the server
      alert(result);
    }
  });
Musa
  • 96,336
  • 17
  • 118
  • 137
  • When I try this the success alert shows but the php does not fire. – user1352096 Apr 11 '14 at 16:05
  • You need to place the relevant PHP code in place of, or in addition to, your "success" echo. It sounds like whatever PHP code you want to be executed should not output anything else itself (if you want only "success" to be returned as a response). It seems the problem is you're combining the two in the description of your question... both outputting 'success' and the remainder of the default content on the page. – Marc Kline Apr 11 '14 at 16:22
  • So I've simplified the php code even further it now simply does a Get request for newid and tries to echo the variable to the screen. I now get an undefined error – user1352096 Apr 11 '14 at 16:52
0

I think the problem is that you're a little mixed up about what you're trying to accomplish here or how you should do it.

Try

if(isset($_GET['newid'])){
  // Do whatever you want when the script gets an AJAX request

  // You want to exit early to avoid having the whole page show up in the alert()
  exit;
}

// Your normal, non-AJAX PHP code goes here

Note that your page is coming back in its entirety in the alert() because you aren't exiting early, which you probably want to do for an AJAX response. The way you have it setup, the whole page is sent back as if you're requesting it from a browser viewport.

Marc Kline
  • 9,399
  • 1
  • 33
  • 36
  • You could also wrap your non-AJAX code in an else{} block instead of exiting early if the condition is met. – Marc Kline Apr 11 '14 at 16:16
  • I've tried this but the alert still returns the entire page code. – user1352096 Apr 11 '14 at 16:21
  • Then something is missing from your description of the problem I think. Try @Musa's code, but place the PHP code you want to run when an AJAX request is made inside of the same block where you output "success". But again, don't put the entire page's code inside of that blog... just what you want to run when an AJAX call is made. Make sure there's nothing else in there that will output anything. – Marc Kline Apr 11 '14 at 16:26