anthonyJ Posted November 7, 2014 Share Posted November 7, 2014 I am having a helluva time trying to populate my state option box with a state array.I have the array set but now how does this get called into the foreach block where the select box is? I can populate it i guess with options manually but I want to get the array to display in the box instead.I tried everything with the minimal experience I have so I need a little help, I got the select box up and displaying where I want it but I cant populate it with an array call. See foreach block near the end of the code:Thanks a bunch!here is my include file code so far:<?php /* Program name: form_test_state.inc * Description: Defines a form that collects a user's * name and mailing address. */$rows = array( "first_name" => "First Name", "last_name" => "Last Name", "phone" => "Phone Number", "city" => "City", "state" => "State", "address" => "Address", "zip_code" => "ZIP Code", "e_mail" => "E-Mail"); $states_list = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming"); $submit = "Submit mailing information";?><html><head> <style type='text/css'> <!--body { background: #42413C; color: #000; font-family: Tahoma, Geneva, sans-serif; font-size: 90%;} form { margin: 1em 2 2 2; padding: 1; width: 1100px; margin-left: 35%; } .field {padding-bottom: 1em; } label { font-weight: bold; float: left; width: 10%;margin-right: 1em; text-align: right; background-color: #9F6;padding-right: 5px; } select {margin-bottom: 1em;} #submit { margin-left: 15%; }h3 {width: 500px;margin-left: 35%;} --> </style></head><body><h3>Please enter your mailing information below:</h3><?php /* loop that displays the form */ echo "<form action='checkBlankOnly_latest.php' method='post'>"; foreach($rows as $field => $label) if($field == "state") { echo "<label for='$field'>$label</label> <select name='State'> <<<< Insert $states_list array here I am trying to figure out >>>> <option value='state'>Alabama </option></select> "; } else { echo "<div class='field'><label for='$field'>$label</label> <input id='$field' name='$field' type='text' value='".@$$field."' size='25%' maxlength='65' /></div>\n"; } echo "<div id='submit'> <input type='submit' value='$submit'></div>";?></form></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/292331-how-can-i-populate-a-usa-states-select-box-with-an-array-call/ Share on other sites More sharing options...
hansford Posted November 7, 2014 Share Posted November 7, 2014 Try this: echo "<label for='$field'>$label</label> <select name='State'>"; foreach($states_list as $state) { echo "<option value={$state}>{$state}</option>"; } echo " </select> "; Quote Link to comment https://forums.phpfreaks.com/topic/292331-how-can-i-populate-a-usa-states-select-box-with-an-array-call/#findComment-1495984 Share on other sites More sharing options...
cyberRobot Posted November 7, 2014 Share Posted November 7, 2014 Try this: //... foreach($states_list as $state) { echo "<option value={$state}>{$state}</option>"; } //... Note that you'll need quotes around the value attribute's value. echo "<option value='$state'>$state</option>"; Otherwise, states with spaces in the name (like "New York") won't be passed through the form submission correctly. Side note: that <label> tag isn't really doing anything. To connect it with the corresponding form field, you could move the end </label> tag after the end </select> tag. <?php echo "<label>$label"; echo "<select name='State'>"; foreach($states_list as $state) { echo "<option value='$state'>$state</option>"; } echo "</select>"; echo '</label>'; ?> Note that I removed the "for" attribute from the <label> tag since it's no longer needed. More information about the form label, including an example of how to get the "for" attribute to work, can be found here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label 1 Quote Link to comment https://forums.phpfreaks.com/topic/292331-how-can-i-populate-a-usa-states-select-box-with-an-array-call/#findComment-1496007 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.