Hobbyist_PHPer Posted May 16, 2012 Share Posted May 16, 2012 Hi, this is a bit of a PHP and a JQuery question, so I wasn't sure which forum to post it in, but here it is... I need a way to asynchronously change a jQuery variable with PHP when a condition becomes true, without user action. I have the following JQuery: <script src="http://code.jquery.com/jquery-latest.js"></script> <script> finished = false; $(document).ready(function() { setTimeout("Refresh()", 5000); }); if (!finished) { function Refresh() { location.reload(); }; } </script> Then I have the following PHP code accessing the MySQL database every time the page refreshes... Here's that code: <? $query = "SELECT * FROM TempAwaitingClients WHERE PSOID = '{$_SESSION['user_id']}'"; $result = mysql_query($query); $rowCounter = mysql_num_rows($result); if ($rowCounter > 0) { while ($row = mysql_fetch_assoc($result)) { // Condition is true, need to stop the page refreshes echo '<iframe id="audiblealert" name="audiblealert" height="0" width="0" src="telephone-ring-4.wav"></iframe>'; echo '<a href="" target="audiblealert">Mute Ringer</a>'; echo 'Phone Number: ' . $row['ClientPhoneNumber'] . '<br /> Purchased Time: ' . $row['ClientPurchasedMinutes'] . ' minutes<br />'; } } else { echo 'No Clients Yet.'; } ?> Anyone have an suggestions or help? EDIT: I forgot to mention, it's the JQuery variable "finished" that I need to set to TRUE Quote Link to comment https://forums.phpfreaks.com/topic/262613-change-a-jquery-variable-with-php/ Share on other sites More sharing options...
trq Posted May 16, 2012 Share Posted May 16, 2012 finished = <?php echo $somephpvar; ?>; Quote Link to comment https://forums.phpfreaks.com/topic/262613-change-a-jquery-variable-with-php/#findComment-1345920 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 16, 2012 Author Share Posted May 16, 2012 finished = <?php echo $somephpvar; ?>; I hope I understood you correctly, here's what I did, it did not work... : while ($row = mysql_fetch_assoc($result)) { ?> finished = true; <? // Condition is true, need to stop the page refreshes echo '<iframe id="audiblealert" name="audiblealert" height="0" width="0" src="telephone-ring-4.wav"></iframe>'; echo '<a href="" target="audiblealert">Mute Ringer</a>'; echo 'Phone Number: ' . $row['ClientPhoneNumber'] . '<br /> Purchased Time: ' . $row['ClientPurchasedMinutes'] . ' minutes<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/262613-change-a-jquery-variable-with-php/#findComment-1345921 Share on other sites More sharing options...
trq Posted May 16, 2012 Share Posted May 16, 2012 And where exactly is that code is relation to the javascript? Quote Link to comment https://forums.phpfreaks.com/topic/262613-change-a-jquery-variable-with-php/#findComment-1345924 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 16, 2012 Author Share Posted May 16, 2012 Here's the whole page, it's rather small... : <? session_start(); include 'includes/connection.inc'; ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> finished = false; $(document).ready(function() { setTimeout("Refresh()", 5000); }); if (!finished) { function Refresh() { location.reload(); }; } </script> </head> <body> <div> <? $query = "SELECT * FROM TempAwaitingClients WHERE PSOID = '{$_SESSION['user_id']}'"; $result = mysql_query($query); $rowCounter = mysql_num_rows($result); if ($rowCounter > 0) { while ($row = mysql_fetch_assoc($result)) { ?> finished = true; <? // Condition is true, need to stop the page refreshes echo '<iframe id="audiblealert" name="audiblealert" height="0" width="0" src="telephone-ring-4.wav"></iframe>'; echo '<a href="" target="audiblealert">Mute Ringer</a>'; echo 'Phone Number: ' . $row['ClientPhoneNumber'] . '<br /> Purchased Time: ' . $row['ClientPurchasedMinutes'] . ' minutes<br />'; } } else { echo 'No Clients Yet.'; } ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/262613-change-a-jquery-variable-with-php/#findComment-1345927 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 16, 2012 Author Share Posted May 16, 2012 Oh, I think I know what you're getting at... put the query up top and actually write the JQuery variable with the PHP inside of the function, right? Quote Link to comment https://forums.phpfreaks.com/topic/262613-change-a-jquery-variable-with-php/#findComment-1345929 Share on other sites More sharing options...
trq Posted May 16, 2012 Share Posted May 16, 2012 Makes no sense any other way. Quote Link to comment https://forums.phpfreaks.com/topic/262613-change-a-jquery-variable-with-php/#findComment-1345930 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 16, 2012 Author Share Posted May 16, 2012 Yeah, I guess you're right, thanks, just needed a thump in the head to get me started... Thanks again.. Quote Link to comment https://forums.phpfreaks.com/topic/262613-change-a-jquery-variable-with-php/#findComment-1345932 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.