简体   繁体   中英

how do i get the innerhtml from a dropdown option with php

I want to get the text in the <option> of my html code with php (to put inside a database) but the only thing I can fetch is my value.

html code:

<form method="post" action="{{PHP_SELF}}" enctype="multipart/form-data">
    <select name="topics" id="topics" >
        <option value="0">Choose &hellip;</option>
        <option value="1">topic1</option>
        <option value="2">topic2</option>
    </select>
</form>

php code: (results in 1,2 but want topic1 or topic2)

$topic = isset($_POST['topics']) ? $_POST['topics'] : '';

is it possible to get this innerhtml code or should i change the values? Thank you.

Your browser just sends the value of that field across the wire.

What you could do is:

  • Use the same as value as you use as title (see comment by @Blake)
  • Keep the list in PHP as well and read your text from there - like following:

     $values = array(1 => 'topic1', 2 => 'topic2'); $topicName = $values[$_POST['topics']]; 

But you can't get stuff in PHP, that is not sent to the server. You can analyze what's sent from and to the server by opening the developer-tools in your browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM