dora1989 2011-11-08 17:16
浏览 104
已采纳

从php中的select / option标签中提取值?

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-->
  • 写回答

1条回答 默认 最新

  • dongshixingga7900 2011-11-08 17:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了