coupe-r Posted February 2, 2010 Share Posted February 2, 2010 Hey Guys, Attached is a screen shot of what my page looks like. What I am essentially trying to do is select a property and view the tenants for the property. When you check the box to the left of the tenant, the input fields will be inserted into the DB under their Tenant_id. The problem I'm having is the Payment Month and Payment Type drop downs are not outputting correctly. The top 2 in the picture always output their selected="selected" value, but the bottom 2 drop downs output the correct selected value. If I could get help with the first Drop Down, I could do the second. Here is the code for the Payment month Drop Down: $months = array('January '.date('Y'), 'February '.date('Y'), 'March '.date('Y'), 'April '.date('Y'), 'May '.date('Y'), 'June '.date('Y'), 'July '.date('Y'), 'August '.date('Y'), 'September '.date('Y'), 'October '.date('Y'), 'November '.date('Y'), 'December '.date('Y')); $pay_month = "<select name=month_box id=month_box>"; foreach ($months as $key => $name) { //array is zero based, so +1 the month number $key++; $selected = ($key == $month) ? ' selected' : ''; $pay_month .= "<option value=$key$selected>$name</option>\n"; } $pay_month .= '</select>'; Does the name= have to be name=month_box[]??? Thanks in advance. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/190675-reading-drop-down-boxes-as-arrays/ Share on other sites More sharing options...
coupe-r Posted February 3, 2010 Author Share Posted February 3, 2010 Any one have any thought? Link to comment https://forums.phpfreaks.com/topic/190675-reading-drop-down-boxes-as-arrays/#findComment-1005815 Share on other sites More sharing options...
akitchin Posted February 3, 2010 Share Posted February 3, 2010 first off, please PLEASE get quotes around those HTML attributes! it's begging for issues to get into the habit of not using them. second, where is $month coming from, and what format is it in? third, what does the HTML output look like? do you end up with several options that have " selected" at the end of the option tag? Link to comment https://forums.phpfreaks.com/topic/190675-reading-drop-down-boxes-as-arrays/#findComment-1005821 Share on other sites More sharing options...
coupe-r Posted February 3, 2010 Author Share Posted February 3, 2010 $month is an array of months (Jan, Feb, March .....) The HTML output is as follows. 1st line 29 2 select 1 1 2nd line is always correct. It seems the last line always works, but if there are more than 1 lines, only the very last line outputs correctly. 29 = The Tenant ID of the first checkbox, this is correct 2 = Februarys value, but it always outputs 2 no matter which month is selected select = Always outputs select because this is the initial value, regardless of what is selected. 1 = This is outputting fine 1 = This is outputting fine Link to comment https://forums.phpfreaks.com/topic/190675-reading-drop-down-boxes-as-arrays/#findComment-1005830 Share on other sites More sharing options...
akitchin Posted February 3, 2010 Share Posted February 3, 2010 no, $months is an array of the months. $month is the variable you're comparing the $key to in order to check whether it should be the selected option or not. also, i meant the HTML as in the HTML code that the PHP script outputs. Link to comment https://forums.phpfreaks.com/topic/190675-reading-drop-down-boxes-as-arrays/#findComment-1005832 Share on other sites More sharing options...
coupe-r Posted February 4, 2010 Author Share Posted February 4, 2010 Here is the entire code. I just need the names of the drop down boxes to be different for each record so I can capture them. It seems like its only registering the last records input. Is this the right way to do it? $results1 = ''; while($row = mysql_fetch_array($result)) { $id = $row['property_id']; $t_id = $row['tenant_id']; $address = $row['prop_address']; $city = $row['prop_city']; $state = $row['prop_state']; $zip = $row['prop_zip']; $rent = '$ '.$row['prop_rent']; $tenant = $row['ten_lastname'] . ', '.$row['ten_firstname']; if(isset($row['apt']) && !empty($row['apt'])) { $apt = 'Apt '.$row['apt']; $ten_address = $address.', '.$apt.', '.$city.', '.$state.', '.$zip; } else { $ten_address = $address.', '.$city.', '.$state.', '.$zip; } $months = array('January '.date('Y'), 'February '.date('Y'), 'March '.date('Y'), 'April '.date('Y'), 'May '.date('Y'), 'June '.date('Y'), 'July '.date('Y'), 'August '.date('Y'), 'September '.date('Y'), 'October '.date('Y'), 'November '.date('Y'), 'December '.date('Y')); $pay_month = "<select name=month_box[] id=month_box>"; foreach ($months as $key => $name) { $key++; $selected = ($key == $month) ? ' selected' : ''; $pay_month .= "<option value=$key$selected>$name</option>\n"; } $pay_month .= '</select>'; $results1 .= '<table width="100%" border="0" cellpadding="5" cellspacing="0" class="underline">'; $results1 .= '<tr>'; $results1 .= '<td width="4%" align="center">'.'<input type="checkbox" name="checkbox[]" id="checkbox" value="'.$t_id.'"/>'.'</td>'; $results1 .= '<td width="36%" align="left">'.$tenant.'</td>'; $results1 .= '<td width="13%" align="center">'.$pay_month.'</td>'; $results1 .= '<td width="15%" align="center"> <select name="type_box[]" id="type_box"> <option value="select" selected="selected">Select Payment</option> <option value="check">Check</option><option value="cash">Cash</option> <option value="credit">Credit Card</option> <option value="other">Other</option> </select> </td>'; $results1 .= '<td width="26%" align="center"> <input type="radio" name="radio" id="radio" value="1" />Full <input type="radio" name="radio" id="radio2" value="2" />Partial $ <input name="payment_txt" type="text" id="payment_txt" size="8" maxlength="8" /> </td>'; $results1 .= '<td width="6%" align="center"> <input type="checkbox" name="checkbox1[]" id="checkbox1"/> </td>'; $results1 .= '</tr>'; $results1 .= '</table>'; } } Link to comment https://forums.phpfreaks.com/topic/190675-reading-drop-down-boxes-as-arrays/#findComment-1007024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.