Form validation error

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:

<!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:

<?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!');
	}
}
?>

And of course here is the entire code - put together:

<!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>

Finally here’s the Warning by php:

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?

I do basically the same thing as you are doing with some of my forms. Where I differ from you is that I created a separate template that contains only the form elements. Then when my validation script is called, if something is wrong the validation script can re-display the form template with the user submitted values already input on the form.

When the user clicks to submit your form, validation.php is called. OK. If there is a problem, then what? You either need to redirect them to the original form or reload the form template on validation.php.

If you redirect them back to the original form, you are going to have to store the values they submitted somehow. You can store them in Session variables. On your original form you can test to see if the Session variables are set and if so, you can re-load the form with them. (I think…)

You either have to break up your form into a separate template which can be called from validation.php or store your user submitted data in Session variables.

As for your “in_array()” error, it is exactly what it says it is. Your $error variable is not an array. It is a string variable. $error is not an array so you have no reason to use in_array().

It would also help if you let us know what type of validation you are doing in your validation script.

I’m not sure what you are doing with $_POST[‘submit’].

Here what I’m trying to do is to detect when user clicks on submit button causing the code inside the curly braces to run upon evaluating to true and I believe it works.

But I think you are right regarding the array… I want to change $error to

$error = Array();

so it is an array.

I’m also trying to separate the code… create a template & a php controller script, as you suggested but I’m not sure how I’m going to cause errors to display in template though… will see.

It might be easier if you use session variables.

How I do it: my form web page includes the form template which consists of the form itself. When the form is submitted the validation script is called. If there is a problem, the script sets error variables and then reloads the form to display to the user with the submitted values already filled in.

As for the errors displaying in the template, in your validation script you set a variable to indicate an error with a particular value.

$form[‘name’][‘error’] = true;
$form[‘name’][‘value’] = $_POST[‘name’]

Then on your form template you check to see if there is an error with each of the submitted fields, in this case ‘name’. Kind of like how you are doing it now.

<input type=“text” name=“name” <?php if ($form[‘name’][‘error’]): ?> value=“<?php echo $form[‘name’][‘value’]; ?>” /><br/>
<?php endif;?>

I have not persisted form data using Sessions. However, if I were to do what I did over again, I think I would go that route. Then if there is a validation error, you could set the session variables, redirect the user back to the form, and in the form you could fill in the submitted values if they are set.

I am just learning PHP so could be widely missing the point but…

In this piece of code:

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 ' ;
}

You are testing to see that the submit button has been pressed and have retrieved those details from the form and translated into variables. You are then checking that these variables have been set. Would it not be the case that if the user has submitted an empty box that they would be set, but empty? You could say, for example,

if($name == “”){
set a variable appropriately
}

Or use a regular expression to test for a name.

As I say, please take comments with a pinch of salt as I may have got the wrong end of the stick!

Susan