PHP form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mike55555
Forum Newbie
Posts: 2
Joined: Sat Jul 24, 2010 9:21 am

PHP form

Post by mike55555 »

I am looking to set-up a form that has a drop down menu at the top and this decides upon the form. What I am looking to do is have 2 forms (book us, contact us) which both have required form parts and both have Captcha, however I want diffrent fields to be reqired on each one - or each form to show diffrent fields (ie I dont want to know an address if contacting, but will if booking)

Any ideas how to do this?
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: PHP form

Post by oscardog »

The easiest thing to do would be to use JavaScript. Create two HTML forms and put them in two divs, like so:

Code: Select all

<select name="form_options" id="form_options" onchange="javascript: showForm()">
<option value="booking">Booking</option>
<option value="contact">Contact</option>
</select>

<div id="contact_form" style="display: none;">
<!-- Put form html here -->
</div>

<div id="booking_form" style="display: none;">
<!-- Put form html here -->
</div>
Then in the HEAD section of the page, add this:

Code: Select all

<script type="text/javascript">
function showForm() {
var selectedForm = document.getElementById("form_options").options[document.getElementById("form_options").selectedIndex].value;

if(selectedForm == "booking") {
document.getElementById("contact_form").style.display = "none";
document.getElementById("booking_form").style.display = "block";
} else {
document.getElementById("contact_form").style.display = "block";
document.getElementById("booking_form").style.display = "none";
}
}
</script>
Just so you know that's possibly not the best way of doing it but for your purposes it should do nicely. I haven't checked the syntax or that it works though.

Edit: I tested it. It works perfectly.
mike55555
Forum Newbie
Posts: 2
Joined: Sat Jul 24, 2010 9:21 am

Re: PHP form

Post by mike55555 »

Thanks so much for that.
Post Reply