从数据库中检索然后在下拉列表中显示

时间:2013-05-17 20:25:25

标签: php

我创建了一个小脚本,允许用户从数据库中检索数据的下拉列表中进行选择。我的问题是信息检索没有用户选择的选项我希望下拉列表显示一个选项从下拉菜单中选择并显示类似您选择的1)姓名和2)姓氏我还想检索两行,例如。姓名.....我怎么去展示? 在他/她选择后

我的代码到目前为止

      ////Selectiong from twoo tables
$query = mysql_query("SELECT * FROM selections ORDER BY id ASC") or die(mysql_error());
$result = mysql_num_rows($query);

// If no results have been found or when table is empty
if ($result == 0) {

    echo 'No results have been found.';

} else {

    // Display form
    echo '<form name="form" method="post" action="test.php">';
    echo '<select name="id" id="id">';

    // Fetch results from database and list in the select box
    while ($fetch = mysql_fetch_assoc($query)) {

        echo '<option id="'.$fetch['name'].'">'.$fetch['surname'].'</option>';
    }

    echo '</select>';
    echo '</form>';

}

1 个答案:

答案 0 :(得分:0)

您必须使用客户端脚本来检索用户选择的内容。

您可以使用jQuery

<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
           $(document).ready(function(){
              $('#write_in_div').click(function(){
                 var name    = $('#names').val();
                 var surname = $('#surnames').val();
                 var email   = $('#emails').val();
                 $('#write_in_div').text("Name: "+name+" -  Surname: "+surname+" - Email: "+email);
              });
           });
        </script>
    </head>
    <body>
        <?php require 'content.php';?>
    </body>
</html>

content.php

<?php
    $resource_names = mysql_query("SELECT DISTINCT NAME FROM selections ORDER BY id ASC");
    $names = array();
    while($row = mysql_fetch_row($resource_names)){
        $names[] = $row[0]    
    } 
    $resource_surnames = mysql_query("SELECT DISTINCT SURNAME FROM selections ORDER BY id ASC"); 
    $surnames = array();
    while($row = mysql_fetch_row($resource_surnames)){
        $surnames[] = $row[0];
    } 
    $resource_emails   = mysql_query("SELECT DISTINCT EMAIL FROM selections ORDER BY id ASC");    
    $emails = array();
    while($row = mysql_fetch_row($resource_emails)){
        $emails[] = $row[0];
    }
    if(count($emails) <= 0 || count($surnames) <= 0 || count($emails) <= 0){
        echo 'No results have been found.';
    } else {

        // Display form
        echo '<form name="form" method="post" action="test.php">';

        //Names dropdown:
        echo '<select name="id" id="names">';
        foreach($names as $name) echo "<option id='$name'>$name</option>";
        echo '</select>';

        //Surnames dropdown
        echo '<select name="id" id="surnames">';
        foreach($surnames as $surname) echo "<option id='$surname'>$surname</option>";
        echo '</select>';

        //Emails dropdown
        echo '<select name="id" id="emails">';
        foreach($emails as $email) echo "<option id='$email'>$email</option>";
        echo '</select>';

        echo "<button id='write_in_div'>Click me!</button>";

        echo '</form>';

    }
    ?>