soulkingbrook soulkingbrook - 7 months ago 17
PHP Question

ajax data passing to php not working

Okay so, I've scoured stackoverflow for this answer and have come across several threads talking about how to do this, and well, they just haven't helped me yet.

This is all on one page, so that's probably the big problem. I really don't wanna send the post data to some other page and then redirect back to the one in order to get this to work, but I will if you guys cannot assist me in this endeavor.

Anyway, I have this page and I'm trying to pass data to the php via ajax, and I know that php is a server-side language, so the page would have to be reloaded once the data is passed.

php:

if (isset($_POST['location'])) {
echo $_POST['location'];
echo "hey";
}


jquery:

var whateva = "hello";
$.post('index.php', {'location': whateva}, function(){
//alert(data);
//window.location.reload(true);

});


alert(data); does get it to work and echo out given the isset (and also prints out all of the other html), but that is an alert which isn't practical, especially from a user standpoint. But that means that this ajax function is working. The problem here is that I want the same page to load, just with the $_POST['location'] variable set, so I had the bright idea of just reloading the page as the function in this case, which doesn't work. The isset never succeeds

Any help will be appreciated, besides telling me that combining php and javascript is a horrible idea as I already know that

Edit:

I was told to try making another page to post the data back which still didn't work, here's the code for that (with the main page ajax adjusted to direct it there instead):

window.onload = function(){
var inter = <?php echo json_encode($_POST['location']); ?>;
$.post('index.php', {location: inter});
}


I have tried it with and without quotes around location in the .post. Also I have tried to just have the plain javascript there, without the onload, still nothing. The response on the main page when changed to this

$.post('intermediary.php', {location: whateva}, function(response) {
// Log the response to the console
console.log("Response: "+response);
});


it prints out the html of the hidden page, with the variable filled in (var inter = "hello" instead of having the php there, as it should), so the passing to that page works

Answer

Ok, here's the breakdown.

File one: index.html This file is HTML and Javascript only, and is the page seen by the user. This could be a php page, but it does not need to be. Notice the quotes around the string 'whateva'.

<html><head></head><body>

<script>
  $.post('intermediary.php', {location: 'whateva'}, function(response) {
      // Log the response to the console
      console.log("Response: "+response);
  });
</script>

</body></html>

File two: intermediary.php This file is PHP only. It receives data silently through POST and returns data by echoing it.

<?php
if (isset($_POST['location'])) {
    echo $_POST['location'];
    echo "hey";
} else {
    echo 'No data received!';
}
?>
Comments