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;
  • 写回答

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;
       }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘