0

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

到目前为止我的代码

      ////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>';

}
4

1 回答 1

0

you have to use client side scripting to retrieve what users choose.

You can use 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>

with 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>';

    }
    ?>
于 2013-05-17T20:33:10.340 回答