Alistair4267 Posted September 25, 2013 Share Posted September 25, 2013 Hi All I have the following code which works great but i can't seem to figure out how to get the "result" from the database to be displayed in a alert box. Please could someone point me in the right direction or sugguest some way i could achieve this. <?php $dbhost = 'localhost:3036'; $dbuser = 'Dbuser'; $dbpass = 'Mypassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT page_id FROM page WHERE page_id=112'; mysql_select_db('my_wiki'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Page ID :{$row['page_id']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully\n"; mysql_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/ Share on other sites More sharing options...
cyberRobot Posted September 26, 2013 Share Posted September 26, 2013 To get a message to appear inside a JavaScript alert box, you could do something like this: <?php echo "<script type='text/javascript'>alert('message for the alert box goes here');</script>"; ?> Of course, the code needs to be customized to fit your code. Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451278 Share on other sites More sharing options...
codefossa Posted September 26, 2013 Share Posted September 26, 2013 Instead of using echo inside of your while loop, just put the content into a variable. Also, javascript alerts use \n rather than <br />. It can't display HTML. <?php $content = ""; while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { $content .= "Page ID :{$row['page_id']}" . "\n" . "--------------------------------" . "\n"; } echo ' <script type="text/javascript"> window.addEventListener("load", function() { alert("' . $content . '"); }); </script>'; ?> Though, if you wanted to alert each one separately, you would just open the <script> before the while loop, close it after, and each row would echo out an alert. Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451294 Share on other sites More sharing options...
Alistair4267 Posted September 30, 2013 Author Share Posted September 30, 2013 Hi The above code works a treat (cheers). I have change the "page_id" to page_title but the alert box is coming back a "Null" ?? I can confirm that the .php file is saved as UFT-8 so i am a little stumped on how to resolve this. Cheers Alistair Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451829 Share on other sites More sharing options...
Alistair4267 Posted September 30, 2013 Author Share Posted September 30, 2013 Hi Please ignore the above, i was looking at a different script. I am now getting "Object doesn't support this property or method" Cheers Alistair Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451848 Share on other sites More sharing options...
cyberRobot Posted September 30, 2013 Share Posted September 30, 2013 Please post the updated code. Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451855 Share on other sites More sharing options...
nogray Posted September 30, 2013 Share Posted September 30, 2013 JavaScript does not support multi line strings. If you use the example by Xaotique, you need to use json_encode to make sure the string is a valid JavaScript string e.g. alert(" . json_encode($content) . "); Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451961 Share on other sites More sharing options...
Irate Posted September 30, 2013 Share Posted September 30, 2013 Actually, alert and basically everything else supports the newline escape sequence in JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451966 Share on other sites More sharing options...
nogray Posted October 1, 2013 Share Posted October 1, 2013 (edited) JavaScript supports the new line escape sequence, but the string in the example is created in PHP and will be outputted with new line character (not a new line escape sequence). Edited October 1, 2013 by nogray Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451973 Share on other sites More sharing options...
codefossa Posted October 1, 2013 Share Posted October 1, 2013 He's right. I didn't test it, just threw it up quick. When storing it in a variable, you need to use single quotes to hold the new line so "\n" gets sent rather than the new line character. $works = "one line" . '\n' . "and another"; $doesnt = "one line" . "\n" . "that new line char broke the script"; Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451979 Share on other sites More sharing options...
Irate Posted October 1, 2013 Share Posted October 1, 2013 Of course, my bad, quote issues there. Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451995 Share on other sites More sharing options...
Alistair4267 Posted October 8, 2013 Author Share Posted October 8, 2013 (edited) HI All This is my updated code. <?php $dbhost = 'localhost:3036'; $dbuser = 'Dbuser'; $dbpass = 'MyPassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = "SELECT t.old_text FROM my_wiki.page p INNER JOIN my_wiki.revision r ON p.page_latest = r.rev_id INNER JOIN my_wiki.text t ON r.rev_text_id = t.old_id WHERE p.page_id = 208"; mysql_select_db('my_wiki.text'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data...' . mysql_error()); } $content = ""; while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { $content .= "(text from the page : {$row['t.old_text']}"; } echo ' <script type="text/javascript"> { alert(' . {$content} . '); } </script>'; ?> For some reason nothing happens. Cheers Alistair Edited October 8, 2013 by Alistair4267 Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453024 Share on other sites More sharing options...
Ch0cu3r Posted October 8, 2013 Share Posted October 8, 2013 The value of alert() needs to be wrapped in quotes alert("' . {$content} . '"); Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453025 Share on other sites More sharing options...
Alistair4267 Posted October 8, 2013 Author Share Posted October 8, 2013 Ah thanks for that, I now have the alert box showing up but all it shows is "text from the page:" and no variable I bet i have missed something very simple but i just can't see it Cheers Alistair Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453055 Share on other sites More sharing options...
Solution Ch0cu3r Posted October 8, 2013 Solution Share Posted October 8, 2013 $row['t.old_text'] should be $row['old_text'] Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453058 Share on other sites More sharing options...
Alistair4267 Posted October 8, 2013 Author Share Posted October 8, 2013 Just like to add i now get "Undefined index: t_old_text on line 29 $content .= "(text from the page : {$row['t.old_text']}"; Not sure why ?? Cheers Alistair Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453068 Share on other sites More sharing options...
Alistair4267 Posted October 8, 2013 Author Share Posted October 8, 2013 Oh my God, it's working THANK you Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453069 Share on other sites More sharing options...
cyberRobot Posted October 8, 2013 Share Posted October 8, 2013 (edited) Side note: when fetching results, you could use mysql_fetch_assoc() instead of modifying mysql_fetch_array(). http://www.php.net/manual/en/function.mysql-fetch-assoc.php For example: while($row = mysql_fetch_assoc($retval)) Also, you're probably already aware that the mysql_ functions have been depreciated. If not, you should start looking into a different API: http://www.php.net/manual/en/mysqlinfo.api.choosing.php Edited October 8, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453072 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.