2013-04-07 43 views
2

我嘗試從MySQL過濾數據使用下拉選項在PHP

顯示的下拉菜單選項的一些數據,當用戶選擇選擇美國,然後點擊提交顯示,該頁面將進入下一個頁面,並顯示數據僅適用於美國

這裏是我的test.html

<body> 
    <table border="0"> 
    <tr> 
    <th>test 
    </th> 
    </tr> 
     <tr> 
     <td>Select Foreign Agent Country</td> 
     <td></td> 
     <td> 
     <select> 
     <option value="US">United States</option> 
     <option value="AUD">Australia</option> 
    </select> 
     </td> 
     </tr> 
     <td> 
     <button type="submit"><a href="showDB.php">submit</a></button> 
     </td> 

    </table> 

</body> 

代碼在這裏是我的第二頁showDB.php

<?php 
//connect to server 
$connect = mysql_connect("localhost", "root", ""); 

//connect to database 
mysql_select_db("asdasd"); 

//query the database 
//$query = mysql_query("SELECT * FROM auip_wipo_sample"); 
if($_POST['value'] == 'US') { 
    // query to get all US records 
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='US'"); 
} 
elseif($_POST['value'] == 'AUD') { 
    // query to get all AUD records 
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='AUD'"); 
} else { 
    // query to get all records 
    $query = mysql_query("SELECT * FROM auip_wipo_sample"); 
} 
//fetch the result 
Print "<table border cellpadding=3>"; 
while($row = mysql_fetch_array($query)) 
{ 
    Print "<tr>"; 
    Print "<th>Name:</th> <td>".$row['invention_title'] . "</td> "; 
    Print "<th>Pet:</th> <td>".$row['invention-title'] . " </td></tr>"; 
} 
Print "</table>"; 
?> 

我嘗試上面的代碼,但我得到2錯誤其是

Undefined index: value in C:\xampp\htdocs\folder\showDB.php on line 10 
Undefined index: value in C:\xampp\htdocs\folder\showDB.php on line 14 

線10是 如果($ _ POST [ '值'] == 'US'){

和線14是 ELSEIF($ _ POST [ '值'] == 'AUD'){

誰能給溶液

感謝

回答

0

您需要添加一個name參數的test.html - 它要麼設置爲name="value"使showDB.php沒有任何改動工作,或者設置線10和14相匹配的name參數設置。下面的例子:

編輯: @adeneo是正確的,你還需要添加一種形式,一個有效的提交按鈕

<body> 
    <form action="showDB.php" method="post"> 
    <table border="0"> 
     <tr> 
      <th>test</th> 
     </tr> 
     <tr> 
      <td>Select Foreign Agent Country</td> 
      <td></td> 
      <td> 
       <select name="value"> 
        <option name="country" value="US">United States</option> 
        <option name="country" value="AUD">Australia</option> 
       </select> 
      </td> 
     </tr> 
     <td> 
      <input type="submit" name="btn_submit" /> 
     </td> 
    </table> 
    </form> 
</body> 

和PHP:

if($_POST['country'] == 'US') { 
    // query to get all US records 
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE app_country='US'"); 
} 
elseif($_POST['country'] == 'AUD') { 
+0

感謝您的更正 我試過代碼,但我得到了一個錯誤在同一行「未定義的指數:國家」 – 2013-04-08 13:32:13

+0

你能標記這是公認的答案嗎? – drewwyatt 2013-04-09 03:40:42

+0

如果你真的硬編碼每個字符串?也許查詢應該看起來像'... WHERE app_country =「。$ _POST ['country'];' – George 2016-06-13 21:17:35

1

ÿ您的表單中沒有名稱爲value的元素,因此您也沒有$_POST['value']!其實,你甚至沒有提交表格,你只是在提交按鈕裏面放置了另一個頁面的鏈接?

<body> 
    <form action="showDB.php"> 
     <table border="0"> 
      <tr> 
       <th>test</th> 
      </tr> 
      <tr> 
       <td>Select Foreign Agent Country</td> 
       <td></td> 
       <td> 
        <select name="value"> 
         <option value="US">United States</option> 
         <option value="AUD">Australia</option> 
        </select> 
       </td> 
      </tr> 
      <td> 
       <button type="submit">submit</button> 
      </td> 
     </table> 
    </form> 
</body> 
+0

感謝更正....我仍然是這方面的新手 – 2013-04-08 13:32:43

相關問題