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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算