2013-05-17 46 views
0

我創建了一個小腳本,允許用戶從下拉列表中選擇從數據庫中檢索數據的位置,我的問題是信息檢索沒有用戶選項選擇我希望下拉菜單顯示一個選項,從下拉菜單中選擇並顯示如您選擇了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>'; 

} 
+4

歡迎堆棧溢出! 2件事。你的問題不清楚。你介意改說嗎?和2. [**請不要在新代碼**中使用'mysql_ *'函數](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

回答

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

    } 
    ?> 
+0

這顯示的數據,但它並沒有顯示一個用戶像一個按鈕,它顯示了他/他選擇的數據類型,但它確實填充下拉,我們可以創建一個選項,用戶從下拉菜單中點擊的東西? – Sizwe

+0

抱歉,但我不明白你需要什麼..你能解釋得更好嗎? – JackTurky

+0

我有一個名爲選擇的表格,我想從該表格中檢索以下名稱,姓氏和電子郵件(假設表格中包含許多姓名和電子郵件地址),用戶選擇的字段表示爲例如NAME用戶選定的插孔,對於SURNAME白色然後是EMAIL(at)gmail.com該信息顯示爲Jack White(at)mail.com .....但用戶必須從3個下拉列表中獲取這些值 – Sizwe

相關問題