-1

Ok I have a problem here. I'm trying to get a JS variable to use in an sql query. Now i know that's not possible so i passed the variable using jquery and use the get method to assign it to a variable in php. But the php alwways loads before JS so the sql query doesn't update. Here's there code:

var monthS = calenderMonths[monthNow];
$.get('load2.php', {location:monthS} );

This is in my JS file in a function that's being onloaded in HTML

This is my php:

$monthS = $_GET['location'];

echo "alert($monthS);";
// Connect to MySQL
if ( !( $database = mysql_connect( "localhost",
  "root", "" ) ) )                      
  die( "Could not connect to database </body></html>" );

// open Events database
if ( !mysql_select_db( "Events", $database ) )
die( "Could not open Events database </body></html>" );

$result = mysql_query("SELECT * FROM posted_events WHERE Month_ = '$monthS' ") 
  or die ('Error updating database because: '.mysql_error());

I alert $monthS to see if it passed but the alert ends up empty at first but after clicking 'ok' like 3 secs another alert box comes up with the variable. So I believe the PHP is loading before the variable gets passed. Is there anyway i could solve this? Thanks

Jake N
  • 10,535
  • 11
  • 66
  • 112
Kay
  • 85
  • 11

1 Answers1

2

You should wait for .get() to complete before continuing with the rest of your script (if it relies on the response as yours does).

$.get('load2.php', {location:monthS} )
   .done(function(data) {
   alert(data);
   //rest of your code 
 });
JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
  • so this should be in my php and not js? – Kay Jul 30 '13 at 20:30
  • 1
    No that's the Ajax call in your Javascript, you're basically not understanding your $.get('load2.php', {location:monthS} ); code properly, $.get is a shorthand Ajax call, it runs asynchronously, so your script will continue to run before it receives it's response. In your case you can put the code that needs the variable you receive from your request in the .done() function, so that it only executes once you have received the response from the php page (containing your variable). Read more here: http://api.jquery.com/jQuery.get/ – JohnnyFaldo Jul 30 '13 at 20:36
  • oh, I think I understand now. But I have no other code in the script, everything that i want is being done in the php. But the variable still isn't passing – Kay Jul 30 '13 at 20:41
  • You said that after 3 seconds an alert comes up with the variable? – JohnnyFaldo Jul 30 '13 at 20:43
  • yea that's right. But sql still doesn't see what it's comparing in the database because the query was loaded before I presume – Kay Jul 30 '13 at 20:47
  • I see, and what does load2.php actually do? – JohnnyFaldo Jul 30 '13 at 20:48
  • Searches my database using the variable as a key and i have a JS function that changes/updates some css on my page according to the results. Just for nothing I can't get sql to recognize the variable $monthS – Kay Jul 30 '13 at 20:53
  • can you post all of this code into your question or email me at johnfaldo@gmail.com so I can see what's going on – JohnnyFaldo Jul 30 '13 at 20:55