I am trying to validate a form on an initial screen (JavaScript) clients side and display an alert box if no text has been entered in the textbox. On clicking the OK in the submit alertbox, the webpage returns to the initial screen.
If some text has been entered, the JavaScript must redirect to the PHP file.
So far I have come up with this code:
<HTML>
<HEAD>
<TITLE>Discography Search</TITLE>
<script language="javascript" type="text/javascript" >
function validate(){
var username=document.getElementById('name').value;
if(username.length==0)
{
alert("Enter something in Searchbox");
}
}
</script>
</head>
<BODY>
<H1><center>Search</center></H1>
<FORM ACTION="phpfile.php" METHOD=POST id="submit1">
Search: <INPUT NAME=name id="name"><BR>
Type: <SELECT NAME="dropdown">
<OPTION value="A" ID="A">ARTIST</OPTION>
<OPTION value="A" ID="B">B</OPTION>
<OPTION value="C" ID="C">C</OPTION>
</SELECT>
<br><br><br>
<INPUT TYPE=submit name="submit" onclick="validate()">
<INPUT TYPE=reset>
</FORM>
and the phpfile.php :
<html>
<head><title><center> Search Result</center></title></head>
<body>
<?php
//echo $_POST["name"];
//echo $_POST["dropdown"];
$name=$_POST["name"];
$dropdown=$_POST["dropdown"];
if(empty($name)){
alert("Please enter the something in the searchbox");
}
else{
$url="http://www.allmusic.com/search/".$dropdown."/".$name."/";
echo $url;
//echo '
}
?>
</body>
</html>
However, the part where the null value is checked, the alert box is shown but the phpfile is executed and I get the following error:
Fatal error: Call to undefined function alert() in phpfile.php on line 12
- On clicking the OK in the alert box, how do I stay on the same js page?
- Why can't I include alert() in the js file??
Thanks!
echoit out and then the JavaScript interpreter will execute thealert()when the page is done loading.echo '<script>alert("Please enter the something in the searchbox");</script>';. If you just want to redirect, look into usingheader()(php.net/header())