Jump to content

<select>


Paul-D

Recommended Posts

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"]?>">&nbsp;/&nbsp;

 

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. 

Link to comment
Share on other sites

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 by benanamen
Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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 by Paul-D
Link to comment
Share on other sites

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>

 

  • Like 1
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.