0

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

  1. On clicking the OK in the alert box, how do I stay on the same js page?
  2. Why can't I include alert() in the js file??

Thanks!

2
  • You get the idea wrong. JavaScript is interpreted when the page is loaded. So you would have to echo it out and then the JavaScript interpreter will execute the alert() 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 using header() (php.net/header()) Commented Mar 7, 2013 at 9:47
  • You should learn basic javascript, and PHP tech.pro/tutorial/791/simple-ajax-php-and-javascript Commented Mar 7, 2013 at 10:05

4 Answers 4

4
  1. Do not set an onclick on the button, but set an onsubmit on the form that returns the result of the validate function.

    <FORM ACTION="phpfile.php" METHOD=POST id="submit1" onsubmit="return validate()">
    

    Then return 'false' in the validate function. That will prevent the form from submitting.

  2. You cannot run javascript (which runs in the browser) within PHP (which runs at the server).

Sign up to request clarification or add additional context in comments.

Comments

1

javascript and php are two different things you can not inlcude javascript in php and expect it to work. get the php to spit out a flag to the page e.g. $notvalid. Echo this to the page e.g.

    <script>
 var notvalid = <?php echo $notvalid; ?> 
</script>

Then check the value of the js variable notvalid with javascript.

2 Comments

But how can I do the client side validation and then do the server side validation?
Do the client side validation on the form page - you can intercept the submit event. Do the server side validation when the form is posted and catch the post values.
1

Use it this way

<?php 
//echo $_POST["name"]; 
//echo $_POST["dropdown"]; 
$name=$_POST["name"];
$dropdown=$_POST["dropdown"];
if(empty($name)){
?>
   <script type="text/javascript">
    alert("Please enter the something in the searchbox");
   </script>
<?php
}
else{
    $url="http://www.allmusic.com/search/".$dropdown."/".$name."/";
    echo $url;
    //echo '

}   

?>

Comments

1

You didn't put alert in Javascript code. You put it in PHP code. And PHP does not have alert.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.