Hi,
I have a problem with form validation. I'm getting an error which I don't understand.
Basically I want form to submit to itself & error messages to be displayed if fields that are left blank by user on submission. Could you please tell me where I'm wrong? The following is my code:
HTML form first:
Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Form Validation</title> </head> <body> <h4>Contact Us</h4> <form action="validation.php" method="post"> <fieldset> <legend>Contact Form</legend> Name:<?php if (isset($error) && in_array('name', $error)) { ?> <span>name is required</span> <?php } ?> <br/> <input type="text" name="name" value="<?php echo $_POST['name'] ?>" /><br/> Email:<br/> <input type="text" name="email" /><br/> Comments:<br/> <textarea name="message"></textarea><br/> <input type="submit" name="submit" value="Send" /> </fieldset> </form> </body> </html>
PHP:
And of course here is the entire code - put together:Code:<?php if (isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $error = ''; if (isset($name)) { $error = $error . 'name ' ; } if (isset($email)) { $error = $error . 'email ' ; } if (isset($message)) { $error = $error . 'message ' ; } if (isset($error) && !empty($error)) { echo "<span align='center'>Please fill out the following fields: <b>$error</b></span>"; } else { die('Success!'); } } ?>
Finally here's the Warning by php:Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Form Validation</title> </head> <body> <h4>Contact Us</h4> <?php if (isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $error = ''; if (isset($name)) { $error = $error . 'name ' ; } if (isset($email)) { $error = $error . 'email ' ; } if (isset($message)) { $error = $error . 'message ' ; } if (isset($error) && !empty($error)) { echo "<span align='center'>Please fill out the following fields: <b>$error</b></span>"; } else { die('Success!'); } } ?> <form action="validation.php" method="post"> <fieldset> <legend>Contact Form</legend> Name:<?php if (isset($error) && in_array('name', $error)) { ?> <span>name is required</span> <?php } ?> <br/> <input type="text" name="name" value="<?php echo $_POST['name'] ?>" /><br/> Email:<br/> <input type="text" name="email" /><br/> Comments:<br/> <textarea name="message"></textarea><br/> <input type="submit" name="submit" value="Send" /> </fieldset> </form> </body> </html>
Code:Warning: in_array() expects parameter 2 to be array, string given in C:\wamp\www\form\validation.php on line 61
ALSO: Is it possible to use php's 'header' function so that if all fields are filled to send the data to another page where I can display them?






Bookmarks