PHP get dropdownlist select option value

后端 未结 4 1292
醉话见心
醉话见心 2020-12-18 08:14

In my dropdownlist I have two different values for each option. How can I retrieve both? Let me illustrate what I mean.


                        
    
提交评论

  • 2020-12-18 08:59

    Another strange way of doing it is:

    <select name="my_ddl">
      <option value="<?php echo $value_Id ?>[<?php echo $value_text ?>]">
        <?php echo $value_text ?>   
      </option>
    </select>
    

    Then when you process it you can do this or maybe even something more simple:

    foreach ($_POST['my_dd1'] as $value_Id => $value_text) {
      $value_Id = $value_Id;
      $value_text = $value_text;
    }
    

    Because php treats the [] as meaning the string is an array and so you instantly have an associative array. I agree though that if you put it there in the first place you ought to be able to just look it up again in the code rather than rely on this.

    0 讨论(0)
  • 2020-12-18 09:04

    You cannot get value_text from POST data. One solution is to populate the hidden field after choosing the option via JavaScript.

    0 讨论(0)
  • 2020-12-18 09:07

    Strictly, this is not possible.

    What you could do is use a delimiter in your value attribute:

    <select name="my_ddl">
      <option value="<?php echo $value_Id ?>|<?php echo $value_text ?>"><?php echo $value_text ?>   
      </option>
    </select>
    

    And...

    <?php    
       list($id, $text) = explode('|', $_POST['my_ddl']);
       //...
    ?>
    
    0 讨论(0)
  • 提交回复
    热议问题