Results 1 to 23 of 23

Thread: How to search results, and post data

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    How to search results, and post data

    I wanted to make a text box, and the user put data into it, hit search, and have it post back to the page the data it found, i cant figure it out, any help?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>

    <?php 
      
    require_once ('connect.php'); 
      require_once (
    'opendb.php'); 
      
    $query "SELECT * FROM search_data"
      
    $result = @mysql_query ($query); 
      
    $count mysql_num_rows($result); //number of results 

      
    if ($count 0){ 
        echo 
    "found $count results.<br /><br />\n"
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){ 
          echo 
    ' <tr><td align="left">' $row['zipcode'] . '</td><td align="left">' $row['redirect_url'] . '</td></tr>'
        } 
      }else{ 
        echo 
    "No results found"
      } 
    ?> 

    </body>
    </html>

  2. #2

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    Ok so looked around and got a few bits of code working however, i cant seem to get this working.

    I dont know how to post the value that was imputted by the client, and have it post its value in the search script.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>

    <form action="index.php" method="post">
    Enter Zip Code: <input type="text" name="zipcode_entered_search" /><br />
    <input type="submit" />
    </form>

    <?php
      
    if($_SERVER['REQUEST_METHOD'] == "POST"){ 
     
        echo 
    $_POST["name"];

      }else{ 

    $sql "SELECT * FROM search_data WHERE zipcode='test'";
    $query mysql_query($sql); 
    $num mysql_num_rows($query); //HERE IS WHERE IT GETS THE COUNT 
    if ($num == "1") { 
         
    $row mysql_fetch_assoc($query); 
         
    $info1 $row['zipcode']; 
         
    $info2 $row['redirect_url']; 
         
    $info3 $row['notes_1']; 
         
    //if you repeat this for each column you need, you have it all sorted out in variables. 
    } else { 
         
    //IT IS GREATER THEN, OR LESS THEN 1 
         
    header("Location: redirect.php"); 

      } 
    ?> 
    </body>
    </html>

  3. #3
    Addicted Member
    Join Date
    May 2006
    Location
    Ithaca, NY
    Posts
    145

    Re: How to search results, and post data

    Just check to see if the $_POST is set rather than using the if statement at the top.

    i.e.

    PHP Code:
    if (isset($_POST['name'])) {
      echo 
    $_POST['name'];


  4. #4

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    Quote Originally Posted by asterix299
    Just check to see if the $_POST is set rather than using the if statement at the top.
    i did try that, but it didnt put the value of the person typed in, into the query.

  5. #5
    Addicted Member
    Join Date
    May 2006
    Location
    Ithaca, NY
    Posts
    145

    Re: How to search results, and post data

    because you didn't tell it to... at least not in the code you posted.

  6. #6

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    here is what i have so far, let me know if i am missing something..

    I cant seem to get it to work..

    i just want a form that they put in a zip code into a box, and hit submit, and it searches the db, and puts the value below it, thats it lol

    thanks again for your help.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="index.php" method="post">
    Enter Zip Code: <input type="text" name="zipcode_entered_search" /><br />
    <input type="submit" />
    </form>

    <?php
      
    if($_SERVER['REQUEST_METHOD'] == "POST"){ 
     
        
    $_POST["zipcode_entered_search"];
        
        
    $sql "SELECT * FROM search_data WHERE zipcode='zipcode_entered_search'";
        
    $query mysql_query($sql); 
        
    $num mysql_num_rows($query); //HERE IS WHERE IT GETS THE COUNT
         
    $row mysql_fetch_assoc($query); 
         
    $info1 $row['zipcode']; 
         
    $info2 $row['redirect_url']; 
         
    $info3 $row['notes_1']; 

      }else{ 
    // nothing here yet.


      } 
    ?> 
    </body>
    </html>

  7. #7
    Addicted Member
    Join Date
    May 2006
    Location
    Ithaca, NY
    Posts
    145

    Re: How to search results, and post data

    ok...

    First, get rid of:

    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"){ 
    and replace it with:

    PHP Code:
    if(isset($_POST['entered_zipcode'])) { 
    That's a little simpler to look at.

    Next, you need to assign the value of $_POST['entered_zipcode'] to a variable and put that variable into the query. Right now you have your mysql query searching for the text 'zipcode_entered_search'.

    To fix this simply do the following:

    PHP Code:
    $zipcode $_POST['zipcode_entered_search'];

    $sql "SELECT * FROM search_data WHERE zipcode='$zipcode'"
    Before you continue with php, I suggest you read a few basic tutorials about get/post variables.
    Last edited by asterix299; Jul 11th, 2007 at 09:13 AM.

  8. #8

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    zipcode_entered_search is the name of my textbox that the person puts the value in.

  9. #9
    Addicted Member
    Join Date
    May 2006
    Location
    Ithaca, NY
    Posts
    145

    Re: How to search results, and post data

    I realized...

    What I'm saying is you didn't pass a variable into the query, you had it search for the string 'zipcode_entered_search' and not the zipcode the person entered.

  10. #10

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    i got ya thanks!

    Is there way to have the page redirected if only 1 result is found?

  11. #11
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: How to search results, and post data

    most of these questions were already answered in the last post you opend

    PHP Code:
    $num mysql_num_rows(query);
    if(
    $num=="1") {
         
    header("Location: http://www.yoursite.com");

    My usual boring signature: Something

  12. #12

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    thanks for all the help i am new at this.

    Is there a way to check for 3 things...
    check for 0 results and diplay "nothing found"
    if 1 result, do a redirect

    and if more then 1 result, then display like i have been?

  13. #13
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: How to search results, and post data

    PHP Code:
    if($num=="0"){
         echo 
    "Nothing Found!";
    }else if(
    $num=="1"){
         
    header("Location: mypage.php");
    }else{
         
    //Echo all records here

    My usual boring signature: Something

  14. #14

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    Quote Originally Posted by dclamp
    PHP Code:
    if($num=="0"){
         echo 
    "Nothing Found!";
    }else if(
    $num=="1"){
         
    header("Location: mypage.php");
    }else{
         
    //Echo all records here


    Thank you very much, ok so if i get 1 result, how do i put the value from the database int he redirect?

    So if i had 1 matching result, how do i put URL_REDIRECT in that so it goes there?

  15. #15

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    I tried your example but it just game me a blank page..here i what i have that works, but when i try to put extra if else's in, it just goes blank for me.

    PHP Code:
    <?php
      
    if($_SERVER['REQUEST_METHOD'] == "POST"){ 
       require_once (
    'connect.php'); 
      require_once (
    'opendb.php'); 
      
      
    $zipcode $_POST['zipcode_entered_search']; 
        
      
    $query "SELECT * FROM search_data WHERE zipcode='$zipcode'"
      
    $result = @mysql_query ($query); 
      
    $count mysql_num_rows($result); //number of results 

      
    if ($count 0){ 
        echo 
    "Located $count result(s).<br /><br />\n"
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){ 
           echo 
    $row['zipcode'] . '<br>' $row['company_name'] . '<br>' $row['redirect_url'] . '<br>' $row['notes_1'] . '<p>'
        } 
      }else{ 
        echo 
    "No results found"
      } 

      }else{ 
        
    // nothing
      

    ?>

  16. #16

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    I tried this also, and it didnt work, just comes up blank page.

    PHP Code:
    <?php
      
    if($_SERVER['REQUEST_METHOD'] == "POST"){ 
       require_once (
    'connect.php'); 
      require_once (
    'opendb.php'); 
      
      
    $zipcode $_POST['zipcode_entered_search']; 
        
      
    $query "SELECT * FROM search_data WHERE zipcode='$zipcode'"
      
    $result = @mysql_query ($query); 
      
    $count mysql_num_rows($result); //number of results 

      
    if ($count == 0){ 
        echo 
    "Nothing"
      } 
      }else if(
    $count == 1){ 
        echo 
    "REDIRECT"
      } 
      }else{ 
        echo 
    "Located $count result(s).<br /><br />\n"
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){ 
        echo 
    $row['zipcode'] . '<br>' $row['company_name'] . '<br>' $row['redirect_url'] . '<br>' $row['notes_1'] . '<p>';
      } } }
    ?>

  17. #17
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: How to search results, and post data

    take away the "@" sign in front of the query
    My usual boring signature: Something

  18. #18

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    Quote Originally Posted by dclamp
    take away the "@" sign in front of the query
    I did, i just get a blank page.

  19. #19
    Addicted Member
    Join Date
    May 2006
    Location
    Ithaca, NY
    Posts
    145

    Re: How to search results, and post data

    Your if statement is completely jumbled up... You're putting two closing braces at each else/elseif statement which is causing it to not work.

    PHP Code:
    <?php 
      
    if(isset($_POST['zipcode_entered_search'])){ 
       require_once (
    'connect.php'); 
      require_once (
    'opendb.php'); 
       
      
    $zipcode $_POST['zipcode_entered_search']; 
         
      
    $query "SELECT * FROM search_data WHERE zipcode='$zipcode'"
      
    $result mysql_query ($query); 
      
    $count mysql_num_rows($result); //number of results 

      
    if ($count == 0){ 
        echo 
    "Nothing"
      } else if(
    $count == 1){ 
        echo 
    "REDIRECT"
      } else{ 
        echo 
    "Located $count result(s).<br /><br />\n"
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){ 
        echo 
    $row['zipcode'] . '<br>' $row['company_name'] . '<br>' $row['redirect_url'] . '<br>' $row['notes_1'] . '<p>'
      } }
    ?>

  20. #20

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    Quote Originally Posted by asterix299
    Your if statement is completely jumbled up... You're putting two closing braces at each else/elseif statement which is causing it to not work.

    PHP Code:
    <?php 
      
    if(isset($_POST['zipcode_entered_search'])){ 
       require_once (
    'connect.php'); 
      require_once (
    'opendb.php'); 
       
      
    $zipcode $_POST['zipcode_entered_search']; 
         
      
    $query "SELECT * FROM search_data WHERE zipcode='$zipcode'"
      
    $result mysql_query ($query); 
      
    $count mysql_num_rows($result); //number of results 

      
    if ($count == 0){ 
        echo 
    "Nothing"
      } else if(
    $count == 1){ 
        echo 
    "REDIRECT"
      } else{ 
        echo 
    "Located $count result(s).<br /><br />\n"
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){ 
        echo 
    $row['zipcode'] . '<br>' $row['company_name'] . '<br>' $row['redirect_url'] . '<br>' $row['notes_1'] . '<p>'
      } }
    ?>

    I tried that, still a blank page.

  21. #21
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: How to search results, and post data

    try this code. If there is nothing being searched "Please Use Search Form Above!" will display

    PHP Code:
    <form action="#" method="POST">
        <input type="text" name="zipcode">
        <input type="submit" name="Search">
    </form>
    <br><br><br>
    <?php
    if(isset($_POST['zipcode'])){
        require_once (
    'connect.php');
        require_once (
    'opendb.php');
        
        
    $zipcode $_POST['zipcode'];
        
        
    $query "SELECT * FROM search_data WHERE zipcode='$zipcode'";
        
    $result mysql_query ($query);
        
    $count mysql_num_rows($result); //number of results
        
        
    if ($count == 0){
            echo 
    "Nothing";
        } else if(
    $count == 1){
            echo 
    "REDIRECT";
        } else{
            echo 
    "Located $count result(s).<br /><br />\n";
            while(
    $row mysql_fetch_array($result)){
                echo 
    "<p>" $row['zipcode'] . '<br>' $row['company_name'] . '<br>' $row['redirect_url'] . '<br>' $row['notes_1'] . '</p>';
            } 
        }
    } else {
        echo 
    "Please Use Search Form Above!";
    }
    ?>
    My usual boring signature: Something

  22. #22

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: How to search results, and post data

    Quote Originally Posted by dclamp
    PHP Code:
    if($num=="0"){
         echo 
    "Nothing Found!";
    }else if(
    $num=="1"){
         
    header("Location: mypage.php");
    }else{
         
    //Echo all records here

    on the refer redirect, can i redirect them to a http://www. address?

  23. #23
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: How to search results, and post data

    yes. header("Location: http://www.mysite.com");
    My usual boring signature: Something

Posting Permissions

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



Click Here to Expand Forum to Full Width