dtrvzd1171 2018-12-13 13:40
浏览 51
已采纳

如何在下拉列表中将所选的PHP会话值设置为onclick

I have the following code.

<?php
     session_start();
?>
 <form action = "index.php" method = "post" name = "test">
    <!--<div id ="custom-select" style="width:200px;" onchange = "favsports()"> -->
       <div class = "custom-select">
        <div class = "select">
           <select id ="custom-select" name = "custom-select" style="width:200px;" onchange = " this.form.submit(); myFunction();">
                <option value="Abteilung auswählen" <?php if(isset($_POST['custom-select']) && $_POST['custom-select'] == 'Abteilung auswählen' || $_SESSION['custom-select'] == 'Abteilung auswählen') echo 'selected="selected" '; ?> >Abteilung auswählen</option>
                <option value="TL-311" <?php if(isset($_POST['custom-select']) && $_POST['custom-select'] == 'TL-311' || $_SESSION['custom-select'] == 'TL-311') echo 'selected="selected" '; ?> >TL-311</option>
                <option value="TP-271" <?php if(isset($_POST['custom-select']) && $_POST['custom-select'] == 'TP-271' || $_SESSION['custom-select'] == 'TP-271') echo 'selected="selected" '; ?> >TP-271</option>
                <option value="TP-310" <?php if (isset($_POST['custom-select']) && $_POST['custom-select'] == 'TP-310' || $_SESSION['custom-select'] =='TP-310') echo 'selected="selected" '; ?> >TP-310</option>
        </select>
       </div>
      </div>
 </form>
<?php

     if (isset($_POST["custom-select"]))
     {
        $_SESSION['custom-select'] = $_POST["custom-select"];
        //print_r($_SESSION);
     }

?>

I am using the same code in two pages because the selected value in the first page schould still be selected when the user go on the next page. I am also using the following Javascript code because the selected value schould be save and useon the Pages.

function myFunction()
{

   var optione = '<?php echo $_SESSION['custom-select']; ?>';
     if(optione !== '')
     {
        document.getElementById("custom-select").value = optione;
     }
       document.getElementById("insert").value = optione;
       var mytext = optione;
     if (mytext == "Abteilung auswählen")
       { 
          alert("Wählen Sie bitte eine gültige Abteilung!");
          mytext = null;
          file = "TL-311";
        }
     else 
        {
          mytext = optione;
          window.file = mytext;
        }
     return window.file;
}

The code is working but my problem is that: I have to select a value twice before setting the selected value and when I want to change the selected value, I must also select the other value twice before setting it as selected. can anyone tell me what I'm doing wrong? or What is actually wrong in my code? Please I am new in PHP and HTML programming.

  • 写回答

1条回答 默认 最新

  • duanjiao3754 2018-12-13 19:44
    关注

    Maybe this will help:

    page1.php

    <?php
        header("Content-type: text/html; charset=utf-8");
        session_start();
    
        // put all your elements into array
        $arr = ['Abteilung auswählen', 'TL-311', 'TP-271', 'TP-310'];
        $options = '';
    
        // make your options 
        foreach ($arr as $elem) {
            $options .= '<option value="'. $elem. '">'. $elem .'</option>';
        }
    ?>
    <form action="page2.php" method = "post">
    <div class = "custom-select">
        <div class = "select">
            <select id ="custom-select" name = "custom-select" style="width:200px;" onchange = " this.form.submit();">
                <?php echo $options; ?>
            </select>
        </div>
    </div>
    

    page2.php

    <?php
        header("Content-type: text/html; charset=utf-8");
        session_start();
    
        // check POST and write in SESSION if POST isset
        // otherwise check SESSION and fill variable $custom-select
    
        if(isset($_POST['custom-select'])) {
            $cust_select = $_POST['custom-select'];
            $_SESSION['custom-select'] = $_POST['custom-select'];
        } else if ($_SESSION['custom-select'] && !empty($_SESSION['custom-select'])) {
            $cust_select = $_SESSION['custom-select'];
        }
    
        $arr = ['Abteilung auswählen', 'TL-311', 'TP-271', 'TP-310'];
        $options = '';
        foreach ($arr as $elem) {
        // here you will check if custom_element is equal to current element
        // if is equal then put 'selected' in that <option>
            if ($elem == $cust_select) {
                $selected = ' selected';
            } else {
                $selected = '';
            }
            $options .= '<option value="'. $elem. '"'.$selected.'>'. $elem .'</option>';
        }
    ?>
    
    
    <form action="" method = "post">
        <div class = "custom-select">
            <div class = "select">
                <select id ="custom-select" name = "custom-select" style="width:200px;" onchange = " this.form.submit();">
                    <?php echo $options; ?>
                </select>
            </div>
        </div>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题