Validating Web Form

I have a web page with two separate web forms on it. I use display:none to hide the second form. When the user selects a check box, the second form becomes visible. I have validation for both forms. Also, both forms use the same form-process.php file.

The problem I have is that when the user clicks the submit button without filling out the second form, my validation prevents them from sending the first one.

How can I make it so that the second form only validates when the user selects to make it visible?


Samuel :eye:

Make it one single form. Make the checkbox a field of the form.
In your PHP validation code, check the value of the checkbox. Validate the fields of the second part of the form only if the checkbox is checked.

Thanks. How do I make it so that the second half of the form validates only when the checkbox has been selected?

Do an if with the checkbox name and value, and put the validation of the second part inside the if.

I am very new to php scripting. Could you give a example of the code?



errmmm. There’s example code and more at the page that guido2004 posted a link to.

Thank you Mittineague. I saw the example code and had trouble applying it. It would help me wrap my mind around it better if the example code was used in the way that I wish to use it.

I have a 2-part form that needs to be validated upon pressing the submit button. I need the second part of the form (id=“auto”) to be validated only when the check box has been selected.



This is my two part form:

<form action="homeowners-form-process.php" method="post" target="_self" id="homeowners-form">
<label><input type="checkbox" name="includeAutoQuote" id="includeAutoQuote" /> Include an auto quote along with your homeowners quote</label>

		<div id="residence">
				<input name="name" type="text" class="text-field-home-narrow" id="name" />
				<input name="address" type="text" class="text-field-home-narrow" id="address" />
			<div id="auto">

			<label>License #<br/>
				<input name="license number" type="text" class="text-field-home-narrow" id="license number" />
				<input name="dob" type="text" class="text-field-home-narrow" id="dob" />
				</label>			</div>

			<label>Year you receved your first license<br/>
				<input name="first license" type="text" class="text-field-home-narrow" id="first license" />
			<label>VIN # (listed on  vehicle registration)<br/>
				<input name="vin number" type="text" class="text-field-home-narrow" id="vin number" />


<div id="submit-button"><input type="image" src="images/submit-button.jpg" name="submit" id="submit" value="Submit" />


Well, first off, your mark-up as you posted it doesn’t look well-formed. i.e. the tags (the div tags) don’t look like they’re nested correctly. So you should fix that. Then give the checkbox input a value attribute with a value of your choice eg. ‘do_quote’.

Then add something like this into your PHP code

if ( isset($_POST['includeAutoQuote']) && ($_POST['includeAutoQuote'] == 'do_quote')  )
// validate second part