Jump to content

Javascript alert


Alistair4267

Recommended Posts

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/282442-javascript-alert/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451294
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451829
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451979
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453024
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1453072
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.