filter data to show using dropdown option in php

15,523

Solution 1

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

Solution 2

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>
Share:
15,523
Anonyqkaskdn Klnasklw
Author by

Anonyqkaskdn Klnasklw

Updated on June 04, 2022

Comments

  • Anonyqkaskdn Klnasklw
    Anonyqkaskdn Klnasklw almost 2 years

    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

  • Anonyqkaskdn Klnasklw
    Anonyqkaskdn Klnasklw about 11 years
    thanks for the correction I tried the code but I got an error in same line "Undefined index: country"
  • George
    George almost 8 years
    Should you REALLY hardcode every string? Maybe the query should look like ... WHERE app_country=" . $_POST['country'];