Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-07-2013, 04:55 PM   PM User | #1
jennypretty
Regular Coder

 
Join Date: Nov 2005
Posts: 225
Thanks: 2
Thanked 0 Times in 0 Posts
jennypretty is an unknown quantity at this point
PHP option selection help

Hello,

I tried to create a drop-down for users to pick. If a user pick 'United States', then Us States will show up. Otherwise, 'Outside usa' will show.

It didn't work. Here is my code:

dt>Country</dt>
<dd>
<select class="select" id="field_country" name="country">
<option value="1" selected="selected">United States</option>
<option value="2">Canada</option><option value="3">Afghanistan</option>
</select>
</dd>
<?php if (field_country) == "1") { ?>
<dt>State</dt>
<dd>
<select class="select" id="field_state" name="state">
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="5" selected="selected">California</option>
</select>
</dd>
<?php } else { ?>
<dt>State</dt>
<dd>
<select class="select" id="field_state" name="state">
<option value="6" selected="selected">Outside Usa</option>
</select>
</dd>
<?php } ?>

Thanks.
jennypretty is offline   Reply With Quote
Old 01-07-2013, 05:08 PM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Check how you've written your variable - You've forgotten the $:

PHP Code:
<?php if (field_country) == "1") { ?>
<dt>State</dt>
Should be:

PHP Code:
<?php if ($field_country) == "1") { ?>
<dt>State</dt>
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 01-07-2013, 06:00 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is incorrect as well. That should be:
PHP Code:
<?php if ($field_country == 1) { ?>
Without the closing ) there. 1 and "1" are considered the same values with the equality check.
Just a heads up though, this of course will not change what is actually shown upon selection. You need to use JS for that. As you have it right now, it will show a selected "United States", followed by a field for the "Outside USA" option. It would be better to split them up to force one, then force another. Can't do a whole lot code wise since we don't know what your form's are doing, but you'd need to self post it back to get that data.
PHP Code:
<dt>Country</dt>
<dd>
<select class="select" id="field_country" name="country">
<option value="1" selected="selected">United States</option>
<option value="2">Canada</option><option value="3">Afghanistan</option>
</select>
</dd>
<dt>State</dt>
<dd>
<select class="select" id="field_state" name="state">
<?php
if (!isset($_POST['field_country']) || isset($_POST['field_country']) && $_POST['field_country'] == 1)
{
?>
    <option value="1">Alabama</option>
    <option value="2">Alaska</option>
    <option value="3">Arizona</option>
    <option value="5" selected="selected">California</option>
<?php
}
else
{
    print 
'<option value="6" selected="selected">Outside USA</option>';
}
?>
</select>
</dd>
Like that. That way on first load it will assume no post has been send back to indicate the first options, then the second will take the !isset check and give the states. It can be much better written though, PHP wise you may be better off never presenting the options until its been selected, then walk the form through. AJAX wise it can be done on the fly.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:21 AM.


Advertisement
Log in to turn off these ads.