Php validation

The following php form validation code does not work properly. Can anyone figure this out? Is it a duplicate attribute issue?


if ($_POST['submit'])


$name = $_POST['name'];
$phone = $_POST['phone'];

$errorstring = "";

if (!$name)
	$errorstring = $errorstring."name<br>";

if (!$phone)
 	$errorstring = $errorstring."phone<br>";

if ($errorstring!="")
	echo "Please fill out the following fields:<br>$errorstring";
  //run code


I believe this is the part of the code.

if ( isset($_POST['includeAutoQuote']) && ($_POST['includeAutoQuote'] == 'includeAutoQuote')  )

You can’t handle checkboxes likeyou are text inputs. When a checkbox is not checked a value for it doesn’t exist. So what you need to do is see if a value exists and if not use a placeholder to differentiate unchecked from checked.

$cb = isset($_POST['cb']) && strcmp($_POST['cb'],$expected) == 0?$_POST['cb']:null;

It sounds like the logic of the if/else tests might be a bit messed up. Can you post that part of the PHP code?

Thanks! It successfully checks the box on the reloaded page. For some reason the rule that states if the checkbox has been checked then the auto section of the form will expand does not work, but when I uncheck the checkbox on the reloaded page the auto form appears, which is the opposite of what it should do.

Do I change the “cb” to something else? Where do I place this code?


The $errorstring variable value can be reassigned when you want it to be appended to. Use “.=” instead of “=” in your ifs.

Thanks. I finally got this to work. How can I make the checkbox stay checked after the page reloads?

Get the value of the checkbox’s attribute and if it’s checked pass that to the reloaded checkbox’s attribute.

How would the code for this look?

What it would look like depends on what you want it to do. What do you have now?

When a visitor submits the form without filling in the required information it reloads the same page upon them pressing submit. However, when the page loads after them pressing submit the checkbox is no longer checked. There is a php rule that states if the checkbox is not checked the form will not validate or send properly, so when they send the form again after filling in the fields they missed the first time the form doesn’t send properly. I need the checkbox to be checked on the page that follows after the user presses submit the first time on the form. Here is some of the form code:

<form action="multi-insurance-quote-test.php" method="post" id="auto-form">
<input type="checkbox" name="includeAutoQuote" id="includeAutoQuote" value="includeAutoQuote" />
				<div id="form-bottom-left">			<label>Name<br/>
					<input name="name" type="text" class="text-field-home-narrow" id="name" value="<?php echo $name; ?>" />
						<input name="address" type="text" class="text-field-home-narrow" id="address" />
				<div id="form-bottom-right">
					<label>Phone #<br/>
						<input name="phone" type="text" class="text-field-home-narrow" id="phone" value="<?php echo $phone; ?>" />
						<input name="email" type="text" class="text-field-home-narrow" id="email" />
<div id="submit-button"><input type="image" src="images/submit-button.jpg" name="submit" id="submit" value="Submit" /></form>

Sorry to have been gone, I got swamped all of a sudden for a couple of days.

Anyway, the form has the method “post”. That means input values will be put into PHP’s $_POST array.

To get their values, use their name as the array key eg.

$name = $_POST['name'];

To put the same value back into the form you can do something like you are doing

<input name="name" type="text"
 id="name" value="<?php echo $name; ?>" />

So for the checkbox it could be something like

<input type="checkbox" name="includeAutoQuote"
 id="includeAutoQuote" value="includeAutoQuote"
 checked="<?php if( isset($_POST['includeAutoQuote']) ) echo 'checked'; ?>" />