Paul-D Posted December 5, 2018 Share Posted December 5, 2018 Hi I have some website code that has worked fine up to now. I enter a day / month and / year. I tried to change the txt box for the day into a drop down list. and the submission $_POST fails. This works <input style="text-align:right;" type="text" size="1" maxlength="2" name="DD_Balance" value="<?=$_SESSION["DD_Balance"]?>"> / This doesn't <select name="DD_Balance" style="width:40px"> <? for($days=1;$days<32;$days++) {?> <option value=$days <? if ($_SESSION["DD_Balance"] == $days){?> SELECTED <?}?>><?=$days?> <? } ?> </select> Confused as the names of the objects are the same. Also view text is all screwed up on several lines not underneath each other. Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 5, 2018 Share Posted December 5, 2018 (edited) You are missing the rest of the Opening Php tags and missing the closing option. You could write this much cleaner using range. <select name="DD_Balance" style="width:40px"> <?php foreach (range(1, 31) as $number) { echo "<option value='$number'>$number</option>"; } ?> </select> Edited December 5, 2018 by benanamen Quote Link to comment Share on other sites More sharing options...
Barand Posted December 5, 2018 Share Posted December 5, 2018 (edited) Try cutting down on all those entry/exits to php <select name="DD_Balance" style="width:40px"> <?php for ($day=1; $day<=31; $day++) { $sel = $_SESSION["DD_Balance"] == $day ? 'selected' : ''; echo "<option value='$day' $sel>$day</option>\n"; } ?> </select> [edit] ... or <?php function dayOptions() { $opts = ''; for ($day=1; $day<=31; $day++) { $sel = $_SESSION["DD_Balance"] == $day ? 'selected' : ''; $opts .= "<option value-'$day' $sel>$day</option>\n"; } return $opts; } ?> <html> <head> <title>Sample</title> </head> <body> <select name="DD_Balance" style="width:40px"> <?=dayOptions()?> </select> </body> </html> Edited December 5, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 5, 2018 Share Posted December 5, 2018 When making a function out of it as @Barand has shown I would pass the comparison value to the function to get more use out if it. You may not always be getting the value from a Session. It is highly likely that it could also come from a database result. Rather than post how to do it, it would be good to review the manual page. Quote Link to comment Share on other sites More sharing options...
Paul-D Posted December 6, 2018 Author Share Posted December 6, 2018 (edited) Hi thanks for your suggestions. I should have used another inline php <option value="<?=$days?>" BUT your echo and keep everything inside PHP script better and I have never used foreach (range(1, 31) as $number ALSO when I use a loop to create the days drop down and view source i get... <select name="DD_Balance" style="width:40px"> <option value="1" >1<option value="2" >2<option value="3" >3<option value="4" >4<option value="5" SELECTED >5<option value="6" >6<option value="7" >7<option value="8" >8<option value="9" >9<option value="10" >10<option value="11" >11<option value="12" >12<option value="13" >13<option value="14" >14<option value="15" >15<option value="16" >16<option value="17" >17<option value="18" >18<option value="19" >19<option value="20" >20<option value="21" >21<option value="22" >22<option value="23" >23<option value="24" >24<option value="25" >25<option value="26" >26<option value="27" >27<option value="28" >28<option value="29" >29<option value="30" >30<option value="31" >31</select> But the months that have been done manually are <select name="MM_Balanc style="width:60px"> <option value="1" >Jan <option value="2" >Feb <option value="3" >Mar <option value="4" >Apr <option value="5" >May etc Edited December 6, 2018 by Paul-D Quote Link to comment Share on other sites More sharing options...
Barand Posted December 6, 2018 Share Posted December 6, 2018 Your <option> tags should ech have a correponding </option> closing tag. Your name="MM_Balance should have closing quote. No need to do the months manually: <?php function monthOptions($current) { $opts = ''; $dt1 = new DateTime('2018-01-01'); $dp = new DatePeriod($dt1, new DateInterval('P1M'), 11); foreach ($dp as $d) { $sel = $d->format('m')==$current ? 'selected' : ''; $opts .= sprintf("<option value='%d' $sel>%s</option>\n", $d->format('m'), $d->format('M') ); } return $opts; } ?> <select name="MM_Balance" style="width:60px"> <?=monthOptions(12)?> </select> 1 Quote Link to comment 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.