View Full Version : Showing and Hiding Form Fields

12-16-2008, 11:01 PM
I have a form that is somewhat long, so I'd like to only show certain fields if they are necessary, but my code isn't working. Is there an easier way to do this, or do I just need to fix something??

What I want here is a selection box where users can choose to be paid via paypal or by check.

<select name="payment">
<option value="" <?php if($_POST["payment"] == ""){ echo('selected="selected"'); } ?>>Select Payment Method</option>
<option value="Check" <?php if($_POST["payment"] == "Check"){ echo('selected="selected"'); } ?>>Check</option>
<option value="Paypal" <?php if($_POST["payment"] == "Paypal"){ echo('selected="selected"'); } ?>>Paypal</option>
Please select the method you would like:

<?php if($_POST["payment"] == "Check"){
echo('check details and fields will go here if applicable'); }
if($_POST["payment"] == "Paypal"){
echo('paypal information will go here if applicable'); }
if($_POST["payment"] == ""){
echo('make a selection</font>');}

12-16-2008, 11:10 PM
What isn't working?

12-16-2008, 11:48 PM
Instead of showing what's selected, the menu selection menu is on "paypal" and it just says "'paypal information will go here if applicable" underneath.

It looks like it's just setting the variable to paypal.

12-17-2008, 01:22 AM
I see... forgive the obvious question, but is the value of $_POST['payment'] = "Paypal"? Because that seems to be the only way it would select Paypal as the default. If $_POST['payment'] shouldn't be = "Paypal", then you'll need to explain how it gets set to something different and where it then gets changed to Paypal. From the small snippet of code you're showing, I can't tell what could be wrong.

12-17-2008, 08:32 PM
The code I was using is a javascript that basically says show or hide the field based on what is selected from a dropdown. I found the problem was that there was only the option to show or hide. What I'm looking for is a snippet or something where I can specify what is shown based on selection.

Like if you had a Country Selection box and say I pick USA, then it shows "states" instead of say "provinces" or "territories" etc..

12-17-2008, 10:37 PM
if you want the options updated without reloading the page (ie clicking a submit button) you'll have to use javascript as php requires a page refresh.

You might be able to find some sample code on the jquery.com site.