Jump to content


Photo

Javascript alert

mysql javavscript php

Best Answer Ch0cu3r, 08 October 2013 - 07:11 AM

$row['t.old_text'] should be $row['old_text']

Go to the full post


  • Please log in to reply
17 replies to this topic

#1 Alistair4267

Alistair4267

    Newbie

  • New Members
  • Pip
  • 7 posts

Posted 25 September 2013 - 02:52 PM

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



#2 cyberRobot

cyberRobot

    Advanced Member

  • Gurus
  • 1,592 posts

Posted 26 September 2013 - 07:48 AM

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.

Blogging about PHP and other web development topics at ScorpBytes.com

#3 Xaotique

Xaotique

    Advanced Member

  • Members
  • PipPipPip
  • 558 posts
  • LocationPennsylvania, United States
  • Age:21

Posted 26 September 2013 - 10:17 AM

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.



#4 Alistair4267

Alistair4267

    Newbie

  • New Members
  • Pip
  • 7 posts

Posted 30 September 2013 - 04:58 AM

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



#5 Alistair4267

Alistair4267

    Newbie

  • New Members
  • Pip
  • 7 posts

Posted 30 September 2013 - 06:43 AM

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



#6 cyberRobot

cyberRobot

    Advanced Member

  • Gurus
  • 1,592 posts

Posted 30 September 2013 - 07:21 AM

Please post the updated code.


Blogging about PHP and other web development topics at ScorpBytes.com

#7 nogray

nogray

    Advanced Member

  • Members
  • PipPipPip
  • 930 posts
  • LocationSan Francisco CA

Posted 30 September 2013 - 04:24 PM

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


NoGray.com


#8 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 30 September 2013 - 05:23 PM

Actually, alert and basically everything else supports the newline escape sequence in JavaScript.
Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#9 nogray

nogray

    Advanced Member

  • Members
  • PipPipPip
  • 930 posts
  • LocationSan Francisco CA

Posted 30 September 2013 - 07:59 PM

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 by nogray, 30 September 2013 - 08:01 PM.

NoGray.com


#10 Xaotique

Xaotique

    Advanced Member

  • Members
  • PipPipPip
  • 558 posts
  • LocationPennsylvania, United States
  • Age:21

Posted 30 September 2013 - 10:46 PM

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



#11 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 01 October 2013 - 03:35 AM

Of course, my bad, quote issues there.
Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#12 Alistair4267

Alistair4267

    Newbie

  • New Members
  • Pip
  • 7 posts

Posted 08 October 2013 - 04:17 AM

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, 08 October 2013 - 04:19 AM.


#13 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,018 posts

Posted 08 October 2013 - 04:32 AM

The value of alert() needs to be wrapped in quotes

        alert("' . {$content} . '");


#14 Alistair4267

Alistair4267

    Newbie

  • New Members
  • Pip
  • 7 posts

Posted 08 October 2013 - 07:06 AM

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



#15 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,018 posts

Posted 08 October 2013 - 07:11 AM   Best Answer

$row['t.old_text'] should be $row['old_text']



#16 Alistair4267

Alistair4267

    Newbie

  • New Members
  • Pip
  • 7 posts

Posted 08 October 2013 - 08:06 AM

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



#17 Alistair4267

Alistair4267

    Newbie

  • New Members
  • Pip
  • 7 posts

Posted 08 October 2013 - 08:07 AM

Oh my God, it's working 

 

THANK you 



#18 cyberRobot

cyberRobot

    Advanced Member

  • Gurus
  • 1,592 posts

Posted 08 October 2013 - 08:28 AM

Side note: when fetching results, you could use mysql_fetch_assoc() instead of modifying mysql_fetch_array().

http://www.php.net/m...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/m...pi.choosing.php


Edited by cyberRobot, 08 October 2013 - 08:28 AM.

Blogging about PHP and other web development topics at ScorpBytes.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com