Scolez Posted August 2, 2013 Share Posted August 2, 2013 A variable is declared in index.php when the page loads (random number). When the user clicks on a link, I need to make a MySQL query with the variable. How would I do this? When I ran the code with javascript ($.get("run.php");return false;) it doesn't recognize the variable. I think I have to use Ajax but I just really dont know how that works, I just started PHP and MySQL a few days ago. Help? Quote Link to comment https://forums.phpfreaks.com/topic/280739-help-with-php-variable/ Share on other sites More sharing options...
trq Posted August 2, 2013 Share Posted August 2, 2013 The description of your problem doesn't really help. Can you post some code and try again? Quote Link to comment https://forums.phpfreaks.com/topic/280739-help-with-php-variable/#findComment-1443077 Share on other sites More sharing options...
lemmin Posted August 2, 2013 Share Posted August 2, 2013 If I'm understanding correctly, you need to send the random number back to the server when the user clicks the link? Something like this? <?php if (isset($_GET['rn'])) { mysql_query('SELECT * FROM table WHERE rn = '.$_GET['rn']); } $rn = rand(); echo '<a href="?rn='.$rn.'">random number link</a>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280739-help-with-php-variable/#findComment-1443181 Share on other sites More sharing options...
Pawan_Agarwal Posted August 2, 2013 Share Posted August 2, 2013 $rn = rand(); <?php if (isset($_GET['rn'])){mysql_query('SELECT * FROM table WHERE rn = '.$_GET['rn']);}echo '<a href="?rn='.$rn.'">random number link</a>';?> try this re-arrange code Quote Link to comment https://forums.phpfreaks.com/topic/280739-help-with-php-variable/#findComment-1443190 Share on other sites More sharing options...
Scolez Posted August 3, 2013 Author Share Posted August 3, 2013 Alright guys, I've figured it out. I used Ajax to send the variable with the request. Thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/280739-help-with-php-variable/#findComment-1443317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.