duanci1939
2015-07-08 21:54
浏览 35
已采纳

使用onchange函数PHP,JAVASCRIPT时,选择列表会忽略第一个值

I'm using a select list function below to make a select list that have 5 values inside of it. Values 2-5 works fine when I select them, and they print out their values on the page when I select them, but value 1 does not print out no matter what. I cannot figure out what I did wrong or how to fix it. Please take a look at my 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;

图片转代码服务由CSDN问答提供 功能建议

我正在使用下面的选择列表功能来制作一个包含5个值的选择列表。 当我选择它们时,值2-5工作正常,当我选择它们时,它们会在页面上打印出它们的值,但无论如何,值1都不会打印出来。 我无法弄清楚我做错了什么或如何解决它。 请看一下我的代码:

index.php

 功能限制($ count,$  location){
 
echo“&lt; form method ='POST'action ='$ location'&gt;”; 
echo“&lt; select name ='value'onchange ='this.form.submit()'&gt;  “; 
 
而($ tempCount&lt; $ count){
 $ tempCount ++; 
echo”&lt; option value ='$ tempCount'&gt; $ tempCount&lt; / option&gt;“; 
} 
 
echo”  &lt; / select&gt;“; 
echo”&lt; / form&gt;“; 
 
} 
 
limit(5,”index.php“)
 
 $ value = $ _POST ['value'];  
echo $ value; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglie1898 2015-07-08 22:10
    已采纳

    Add one first option to the < select >, and, check if $_POST['value'] exists. Next is your code with both changes pointed by commented arrows (//<=====) :

    <?php
    
    function limit($count,$location) {
    
    echo "<form method = 'POST' action = '$location'>";
    echo "<select name = 'value' onchange='this.form.submit()'>" .
         "<option>Select an 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 an option" will let the user to choose option 1.

    If you don't want to see "Select an option", the other solution is to make the chosen option selected, for example, if the user chooses "3", when the page reloads the option "3" will be selected, and the user will be able to 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 THE CURRENT OPTION SELECTED IF IT WAS CHOSEN BEFORE. <==========
    if ( isSet( $_POST['value'] ) &&        // IF 'value' EXISTS, AND
       ( $_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;
       }
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题