Select list ignores first value when using the onchange function, PHP, JAVASCRIPT -
i'm using select list function below make select list have 5 values inside of it. values 2-5 works fine when select them, , print out values on page when select them, value 1 not print out no matter what. cannot figure out did wrong or how fix it. please take @ code:
index.php
function limit($count,$location) { echo "<form method = 'post' action = '$location'>"; echo "<select name = 'value' onchange='this.form.submit()'>"; while ($tempcount < $count) { $tempcount++; echo "<option value='$tempcount'>$tempcount</option>"; } echo "</select>"; echo "</form>"; } limit(5,"index.php") $value = $_post['value']; echo $value;
add 1 first option < select >
, and, check if $_post['value']
exists. next code both changes pointed commented arrows (//<=====) :
<?php function limit($count,$location) { echo "<form method = 'post' action = '$location'>"; echo "<select name = 'value' onchange='this.form.submit()'>" . "<option>select option</option>"; // <=========================== while ($tempcount < $count) { $tempcount++; echo "<option value='$tempcount'>$tempcount</option>"; } echo "</select>"; echo "</form>"; } limit(5,"xyz.php"); if ( isset( $_post['value'] ) ) // <=========================== { $value = $_post['value']; echo $value; } ?>
the option "select option" let user choose option 1.
if don't want see "select option", other solution make chosen option selected, example, if user chooses "3", when page reloads option "3" selected, , user able choose option "1" :
<?php function limit($count,$location) { echo "<form method = 'post' action = '$location'>"; echo "<select name = 'value' onchange='this.form.submit()'>"; while ($tempcount < $count) { $tempcount++; // make current option selected if chosen before. <========== if ( isset( $_post['value'] ) && // if 'value' exists, , ( $_post['value'] == $tempcount ) ) // if 'value' == current number $selected = "selected"; else $selected = ""; echo "<option $selected value='$tempcount'>$tempcount</option>"; } echo "</select>"; echo "</form>"; } limit(5,"xyz.php"); if ( isset( $_post['value'] ) ) // <=========================== { $value = $_post['value']; echo $value; } ?>
Comments
Post a Comment