简体   繁体   中英

filter data to show using dropdown option in php

I try to show some data by dropdown option from mySQL

when the user choose option United states and click submit, the page will go to the next page and show the data only for United states

here is my code for 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>

here is my second page 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>"; 
?>

I try the above code, but I got 2 error which are

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

line 10 is if($_POST['value'] == 'US') {

and line 14 is elseif($_POST['value'] == 'AUD') {

anyone can give solution

thanks

You have no element with the name value in your form, so you don't have a $_POST['value'] either! Actually, you don't even have a form to submit at all, you just placed a link to another page inside the submit button ?

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

You need to add a name parameter to test.html - either set it to name="value" to make showDB.php to work without any alterations, or set lines 10 and 14 to match the name parameter you set. Example below:

edit: @adeneo is right, you also need to add a form and a valid submit button

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

and the 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') { 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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