Jump to content

Javascript alert


Go to solution Solved by Ch0cu3r,

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

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.
Link to comment
https://forums.phpfreaks.com/topic/282442-javascript-alert/#findComment-1451278
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

Edited by Alistair4267
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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.