I have been stuck with this bug for a long time now, and i simply have to little knowledge of php to solve it. So i pray for your help.
My problem is, that after i make a post (or ajax) request to my site, the GET request disappears and thus leaving me a NULL object (since i create my object from that id).
I have made some tests, and the problem is at the GET is NULL in the response i got from the POST. And this doesn’t happen if i forget about the get and just initiates the id as a random number.
Here are some parts of my code where i think the problem is:
First is the php with my GET:
require 'php/functions.php';
if (isset($_GET['id'])) {
$id = $_GET['id'];
// $id = 5;
try {
$bar = getBarById($id);
} catch (exception $e) {
exit;
}
After this i output some date from the object and it works all fine, the GET is being initiated here.
Now. When i make my ajax (i tried regular POST also) request, the status response from POST is OK, but after that nothing happens and det response says that my GET request is NULL.
The JS:
<script language="javascript" type="text/javascript">
function save(value) {
$.ajax({
type: "POST",
url: "barpage.php",
data: { save_sum: value }
}).done(function(msg) {
msg.preventDefault();
alert( msg );
});
}
Just for the record. It is the “msg” response from ajax that tells me that nothing happens. and that “$id” is NULL.
What am i doing wrong?
Thanks.
Here’s the deal:
Your AJAX request is doing a POST request, thus the GET array is not being filled, because, well, you’re doing a POST request instead of a GET one. A quick fix would be to send the Id as part of the ajax request and on your php file, ask for it like this:
An alternative solution
An even better solution (from comment bellow)
Hope that helps!