<?php
$hostname = "localhost";
$username = "hi";
$password = "bye";
$dbid = "jesus";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");
$name1=$_REQUEST['name1'];
$result=("SELECT name FROM lydia WHERE nam='$name1'");
while($myrow = mysql_fetch_array($result))
{
$first=$myrow[0];
echo $first;
}
?>
<html>
<head>
<script type="text/javascript">
function win()
{
var c='<?php echo $first; ?>';
alert(c);
}
</script>
</head>
<body>
<form name="form1" method="get" action="<?php echo $PHP_SELF;?>" onSubmit="javascript:win();">
NAME<input name="text" name="name1">
<input type="submit" value="submit">
</form>
</body>
</html>

in this code i want to alert the $first variable.javascript executed before php.is there any solution alert the variable in script.

1. You forgot mysql_query in

$result=("SELECT name FROM lydia WHERE nam='$name1'");

2.

<input name="text" name="name1">

Should have been input type="text".
And, at the first run, ie., when the script is executed for the 1st time, the query will not output anything since $_REQUEST is empty.(or it will output the rows which has an empty nam field). Why not have form method as Post ?

sorry .those two lines which you mentioned are wright in my code,i typed it wrongly in the forum.sorry.i want to know exact error in this code.

Can you post your original code ?

Edit: Btw, for the first execution, $first will be null.. Thats the reason it shows an empty alert.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.