繁体   English   中英

筛选数据以使用php中的下拉选项显示

[英]filter data to show using dropdown option in php

我尝试通过mySQL的下拉选项显示一些数据

当用户选择“美国”选项并单击“提交”时,页面将转到下一页并仅显示美国的数据

这是我的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行是if($ _ POST ['value'] =='US'){

第14行是elseif($ _ POST ['value'] =='AUD'){

任何人都可以给出解决方案

谢谢

您的表单中没有名称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>

您需要在test.html中添加一个name参数-将其设置为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') { 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM