cstrohm Posted October 23, 2007 Share Posted October 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/74500-forms-with/ Share on other sites More sharing options...
cstrohm Posted October 23, 2007 Author Share Posted October 23, 2007 I might have to use Ajax to display the state or prov box without loading the page right? I have no idea Quote Link to comment https://forums.phpfreaks.com/topic/74500-forms-with/#findComment-376508 Share on other sites More sharing options...
manixrock Posted October 23, 2007 Share Posted October 23, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/74500-forms-with/#findComment-376510 Share on other sites More sharing options...
cstrohm Posted October 23, 2007 Author Share Posted October 23, 2007 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/74500-forms-with/#findComment-376525 Share on other sites More sharing options...
Toshiba23 Posted October 24, 2007 Share Posted October 24, 2007 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) Quote Link to comment https://forums.phpfreaks.com/topic/74500-forms-with/#findComment-376804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.