www.webdeveloper.com
Results 1 to 2 of 2

Thread: filter result by selection

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    Exclamation filter result by selection

    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
    PHP Code:
    <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>
            <
    option name="country" value="US">United States</option>
            <
    option name="country" value="AUD">Australia</option>
            </
    select
            </
    td>
          </
    tr>
            <
    td>
            <
    button type="submit" name="btn_submit"><a href="showDB.php">Click</a></button>
            </
    td>
        </
    table>
        </
    form>

    </
    body
    here is my second page showDB.php

    PHP Code:
    <?php
    //connect to server
    $connect mysql_connect("localhost""root""");

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

    //query the database
    $query mysql_query("SELECT * FROM auip_wipo_sample");
    if(
    $_POST['country'] == 'US') {  
        
    // query to get all US records  
        
    $query mysql_query("SELECT * FROM auip_wipo_sample WHERE wipo_applicant1_country='US'");  
    }  
    elseif(
    $_POST['country'] == 'AUD') {  
        
    // query to get all AUD records  
        
    $query mysql_query("SELECT * FROM auip_wipo_sample WHERE wipo_applicant1_country='JP'");  
    } 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>high:</th> <td>".$row['invention_title'] . "</td> "
        Print 
    "<th>lower:</th> <td>".$row['invention-title'] . " </td></tr>"
    }
    Print 
    "</table>";
    ?>
    however, I got 2 error

    Notice: Undefined index: country in C:\xampp\htdocs\fak_ict1999\showDB.php on line 11
    and line 11 is
    Code:
    if($_POST['country'] == 'US') {
    Notice: Undefined index: country in C:\xampp\htdocs\fak_ict1999\showDB.php on line 15
    and line 15 is
    Code:
    elseif($_POST['country'] == 'AUD') {
    anyone can solve this??
    thanks

  2. #2
    Join Date
    Feb 2013
    Posts
    95
    First I think you are making it harder on yourself by combining things onto themselves, if I were going to tackle this I would do.

    1. Fetch the user's input
    2. Build an array/object built off the user's input
    3. Display that array.

    Secondly, this is just a suggestion but use either mysqli or PDO using prepare statements for mysql is depreciated. It will save you a lot of headaches in the long run.

    Here's an idea what I'm talking about (Sorry it's written in Object-Oriented Programming, but you can find how to write mysqli or PDO here - http://www.php.net/manual/en/index.php). For example you can take the last part of the $stmt (Query) and do this 'WHERE country=?' for example.

    PHP Code:
        public function retrieve_comment($id)
        {
            
    /* create a prepared statement */
            
    if ($stmt $this->database->prepare("SELECT id, username, content, thread, post_date FROM comments WHERE  id=?")) {
            
                   
    /* bind parameters for markers */
                   
    $stmt->bind_param("s"$id);
                
                   
    /* execute query */
                   
    $stmt->execute();
                
                   
    /* bind result variables */
                   
    $stmt->bind_result($this->page['id'], $this->page['username'], $this->page['content'], $this->page['thread'], $this->page['post_date']);
                  
                   
    /* fetch value */
                   
    $stmt->fetch();    
                            
                   
    $stmt->close();            
                   
                   return 
    $this->page;
            }
        } 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles