Jump to content

Forms with <if ...


cstrohm

Recommended Posts

Hello,

 

I want to make a form that if any country (drop down) other then canada or the United States is selected then it will not show the state or Province (drop down)

 

If canada is selected I want to show a province drop down and if the united states is selected I want to show a down down for states.

 

for example sake:

 

country is input country

states are input states

province is input prov

 

I am not storing country, states or prov in the database then will be hardcoded in the script.

 

I have the countries in a language file.

 

Here is what I have so far (yes I know there are more then 8 countries :)

 

<select name="country" class="form_select" style="width:90%;">
<option value="<?php echo $lang['country_01']; ?>"><?php echo $lang['country_01']; ?></option>
<option value="<?php echo $lang['country_02']; ?>"><?php echo $lang['country_02']; ?></option>
<option value="<?php echo $lang['country_03']; ?>"><?php echo $lang['country_03']; ?></option>
<option value="<?php echo $lang['country_04']; ?>"><?php echo $lang['country_04']; ?></option>
<option value="<?php echo $lang['country_05']; ?>"><?php echo $lang['country_05']; ?></option>
<option value="<?php echo $lang['country_06']; ?>"><?php echo $lang['country_06']; ?></option>
<option value="<?php echo $lang['country_07']; ?>"><?php echo $lang['country_07']; ?></option>
<option value="<?php echo $lang['country_08']; ?>"><?php echo $lang['country_08']; ?></option>
              </select>

 

This being seen if country_01 is canada and country_02 is United states.

 

What would I put in there so if country_01 was selected then the prov box would show with a list of prov's and same with Country_02 with the list of states.

 

 

 

Thank in advance for a noob learner :)

Link to comment
Share on other sites

first to store large numbers of countries in an array you needn't use keys in the array. So:

 

<option value="<?php echo $lang['country_01']; ?>"><?php echo $lang['country_01']; ?></option>

 

reduced to:

 

<option value="<?php echo $lang[1]; ?>"><?php echo $lang[1]; ?></option>

 

also, you should use the shorthand version, instead of <?php echo $lang[1]; ?> you can do <?php=$lang[1]?> or even shorter <?=$lang[1]?> so you get:

 

<option value="<?=$lang[1]?>"><?=$lang[1]?></option>

 

 

finnally, you should put the whole thing in a loop like:

 

<?php
$lang = array(
    "Armenia",
    "Romania",
    "Usa",
    ...
);
?>
<select name="country" class="form_select" style="width:90%;">
<?php for ($i=0; $i<count($lang); $i++) { ?>
<option value="<?=$lang[$i]?>"><?=$lang[$i]?></option>
<?php } ?>
</select>

Link to comment
Share on other sites

 

<?php
$lang = array(
    "Armenia",
    "Romania",
    "Usa",
    ...
);
?>
<select name="country" class="form_select" style="width:90%;">
[code]<?php for ($i=0; $i<count($lang); $i++) { ?>

<option value="<?=$lang[$i]?>"><?=$lang[$i]?></option>

<?php } ?>

</select>

[/code]

 

This makes since, but again I am just learning :)  can you explain the parts of

<?php for ($i=0; $i<count($lang); $i++) { ?>

 

Also what would I need to add so when the us or canada is selected then a dropdown box with states or prov's show in the form?

 

Link to comment
Share on other sites

use a javascript onChange event for the form...

 

http://www.w3schools.com/jsref/jsref_onchange.asp

 

Use that, if the drop down value is usa or canada, it will display another drop down. w3schools should actually have all the resources to learn how to do it.

 

But technically the question's answer isn't really PHP, unless you want it to be done in a carry-on page (the page after the drop down)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.