简体   繁体   中英

Extract value from select/option tag in php?

I am not exactly how to do it and how to word the question, so i shall try my best (PS: i'm new to web dev, so please be clear in your answers if you could).

So, I have got a drop down menu with the list names, which are taken from my database. In that database i have a table with names column (the ones that are rendered to the dropdown box) and relevant information to those names. Now, I want that relevant information to appear below in a tag when a user choose one of those names. I also cannot use the form.submit() method because my submit button is already taken for something else.

Here is the code to that bit:

<form name="name_choice" method="post" action="index.php">
     <select name="names" onchange="form.some_method()">
      <option value="NULL" selected="selected">--Select name--</option>
       <?php
            for ( $i = 0; $i < $numrows; $i++ ) { //for all the columns, iterate and print out                  
                        $id_names = mysql_result($result, $i);
            echo "<option value='".$id_names."'>".$id_names."</option>";
        }
        ?>
 </select>
 </form>

So the bit above works fine, but the "some_method()" is my problem, i don't know what to trigger to display the text in the div below the drop down box (code is below for it):

<div class="information"> <!--if the name is chosen ONLY!-->
    <?php
    if($_POST['names'] == "NULL") {
         echo '<p>Please select an option from the select box.</p>'; //this bit is for testing
    }
    else {
         echo '<p>You have selected: <strong>', $_POST['names'], '</strong>.</p>';
                 //and then how to echo the relevant information?:(
    }                   
    ?>
</div><!--end of possible info-->

onchange is a JavaScript event. PHP can't do realtime processing of form data, as it sits on the server and the form is on the client. You can sort of do it by using AJAX and passing the form data as the user types, but that would be a lot more work than is needed. Take a look at JavaScript form validation posts to get yourself headed on the correct path.

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