0

I have been staring at this problem for the past 2 hours and can't seem to fathom it, even after validating that everything loads correctly when scouring the console.

I basically have two sliders on my page which will eventually populate results in a table, every time I change my slider I send an array of two values to my AJAX script:

function update_results(values) 
{
   $.ajax({
       type: "GET",
       url:  "./app/core/commands/update_results.php",
       data: { query : values },
       cache: false,
       success: function(data) {
                   // eventually some success callback
       }
   });
}

The browser successfully finds update_results.php but it does not perform the logic on the page ( I assume it has found the page as the 404 error does not appear in my console. )

At this point in time the script is extremely bare-bones as I'm obviously trying to establish communication between both files:

<?php
$vals = $_GET['values'];
echo $vals;

In this case $vals is never echoed to the page, am I missing something in my AJAX? I know the values enter the function as alerted them out before attaching the PHP script.

2
  • If $_GET['values'] is an array, you cannot echo it. To check if it is, use console.log(values); before the ajax. Commented Jun 14, 2014 at 12:28
  • @Ididn'tunderstand... I've tried console logging and var_dump() but neither work Commented Jun 14, 2014 at 12:29

2 Answers 2

2

Ajax Calls are suffering from Browser Cache. If your browser thinks, that he already knows the content of update.php, he will return the cached content, and not trigger the script. Therefore your modified code might simply not get executed. (Therefore your insert query wasn't executed)

To ensure this is not happening in your case, it is always a good idea to pass a parameter (timestamp) to the script, so your browser thinks it's another outcome:

function update_results(values) 
{
   $.ajax({
       type: "GET",
       url:  "./app/core/commands/update_results.php?random_parameter=" + (new Date().getTime());
       data: { query : values },
       cache: false,
       success: function(data) {
                   // eventually some success callback
       }
   });
}

This will ensure that - at least - the browser cache is refreshed once per second for update_results.php, no matter what browser cache-settings or server-side cache advices are telling.

Sign up to request clarification or add additional context in comments.

7 Comments

interesting, I never knew that, I thought by setting cache to false I would bypass that sort of behavior - that would explain why I've been experiencing an undefined index error the past 10 mins, due to one side being GET and the other POST!
@AlexSims Never trust any programatic cache advice. Settings might override it, Server-side advices might simply be ignored. This might not solve your problem - it just makes SURE, that the code you are trying to execute is executed. Your comment about the not working insert query indicates this issue - cause old content is loaded instead of invoking the script, containing the query.
I'll most certainly add that to my toolbox - thanks for the advice :)
hmm very weird - after implementing the timestamp I am still getting an undefined index error Undefined index: values in D:\wamp\www\Kerr Pump - New\app\core\commands\update_results.php on line 5
@AlexSims that's because you are passing your values array with the parameter name query - therefore you have to access it using $_GET["query"].
|
2

when Ajax is done, the success callback is triggered and the output of you php script is saved in data.

you can handle the data like this:

   $.ajax({
       type: "GET",
       url:  "./app/core/commands/update_results.php",
       data: { query : values },
       cache: false,
       dataType: "text",
       success: function(data) {
             document.write( data )
       }
   });

PHP, running at server, is unaware of what happening at the front-end browser and it simply respond to ajax request as any other normal http request. So the failure of SQL query has nothing to do with javascript, which only responsible for sending ajax request and receiving and handling the response. I guess there's some errors in your php script.

2 Comments

thanks Leo that makes sense. However, earlier I tried to run an INSERT query in the php script and nothing was sent to the db, would this also be caused by not writing a success callback?
Yep I think I found the problem, pretty sure its because I didnt check if variables were set - therefore it would just not process the script - thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.