0

I am trying to use a localStorage variable in a php function so I decided to use jquery post() to do so. Afterwards, I want to echo back data to my original file and set it inside a global variable. I have 3 files that connect to each other (its just a bit complicated).

quick_match.php

  dataString = "";
$.post("ajaxCtrl.php", {action: "doQuickMatch", numCard: localStorage['numCard'], UI: "<?php echo $_REQUEST['UI']; ?>"}, function(data){ dataString = data; });

ajaxCtrl.php

if($action == "doQuickMatch")
{
    //Start Quick Match
    $result = $user->getQuickMatch($numCard);
    echo $result;
}

User.php

  (A big query here but I guarantee it works)

  // Perform Query
  $result = mysql_query($query);

  //Hold the array objects in a string
  $string = "";
  // Iterate through results and print
  while ($row = mysql_fetch_assoc($result)) {
    $string .= '{ image: \'http://personals.poz.com'.$row['my_photo'].'\', user_id: \''.$row['id'].'\', user_prof: \'http://127.0.0.1/personalz.poz/v2/member.php?username='.$row['id'].'\'};';
  }
  return $string;

After all of this, I'm supposed to use the string and split it so I can get an array and continue on with what I was doing. However, the callback keeps on returning nothing so I am not sure what I did wrong. Can anyone offer insight to this please? Thank you in advance.

crossfuse999
  • 43
  • 1
  • 9
  • AJAX is asynchronous, it means you don't know when `dataString` will receive your data; – Adib Aroui Jul 10 '15 at 04:46
  • are you sure the request was successful and your callback is being called? An easy way to tell would be to `console.log('data received:', data)` inside your callback. – Andre Gregori Jul 10 '15 at 04:56
  • Ah I keep getting blank when I try to console.log it. Is there any way for me to receive the data then? – crossfuse999 Jul 10 '15 at 05:09
  • This was very informative for me, if you already didn't read it , i advise you do even if it doesn't immediatly resolive your problem: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call. Use callbacks or promises. – Adib Aroui Jul 10 '15 at 05:30

0 Answers0