Page 1 of 1

Javascript alert in php file doesn't display

Posted: Wed Jun 25, 2008 5:58 am
by brettosm8
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I'm working with AJAX from the tutorial at w3schools. I'll use their tutorial to show you my issue.. If you go to http://www.w3schools.com/PHP/php_ajax_database.asp you see the HTML form, the javascript and the php page. I am trying to get the php page to run javascript similarly to this:

Code: Select all

 
<html>
<title></title>
<head></head>
<body>
<script type="text/javascript">
        alert("test");
</script>
<?php
 
$q=$_GET["q"];
 
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }
 
mysql_select_db("ajax_demo", $con);
 
$sql="SELECT * FROM user WHERE id = '".$q."'";
 
$result = mysql_query($sql);
 
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
 
while($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>" . $row['FirstName'] . "</td>";
 echo "<td>" . $row['LastName'] . "</td>";
 echo "<td>" . $row['Age'] . "</td>";
 echo "<td>" . $row['Hometown'] . "</td>";
 echo "<td>" . $row['Job'] . "</td>";
 echo "</tr>";
 }
echo "</table>";
 
mysql_close($con);
?>
</body>
</html>
The alert doesn't display. Once I can make a javascript alert display from this page I'll be able to do what I really want to do i.e. something involving mouseover().

Can someone please help?..


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Javascript alert in php file doesn't display

Posted: Wed Jun 25, 2008 8:04 am
by Jasheppard
try using window.alert("test");

what i know is that for the later versions of HTML and web browsers... they need a page to be specified for the alert, prompt or confirm.

and in the w3schools example, they are calling the JavaScript page with just <script src="blabla"></script>, which is very old.
because having the script tag without any type or language, the browser doesn't know whether its vbs, javascript or any other script types.

i recommended using: <script type="text/javascript" language="javascript">window.alert("test");</script>

hope this all helps. :D

Re: Javascript alert in php file doesn't display

Posted: Thu Jun 26, 2008 7:11 am
by brettosm8
Hi, thanks for the idea. I tried and it still isn't working. Does anyone have any other ideas?

Re: Javascript alert in php file doesn't display

Posted: Thu Jun 26, 2008 8:39 am
by Jasheppard
:lol: do you have JavaScript enabled? rofl

it should work? try the JavaScript in the head instead of the body.

or you could make it a function?
here is a JavaScript alert example on w3chools.com
http://www.w3schools.com/js/tryit.asp?f ... ryjs_alert

Re: Javascript alert in php file doesn't display

Posted: Sun Jul 12, 2009 1:42 pm
by valterekholm
Maybe it would work with <script type='javascript'>.