www.webdeveloper.com
Results 1 to 15 of 15

Thread: php form with drop down!

  1. #1
    Join Date
    Nov 2006
    Posts
    22

    php form with drop down!

    hey, i'll trying to create a search bar on my website where the user can search for a film by the title, director etc using a drop down menu.

    I'll having problems in the action of the form im using, i think i maybe have to link it to a page to detemine which option has been slected by the user from the drop down and then to the page displaying the search results.

    can anyone please help me with the code, im still learning

    heres what i have so far

    <form action="results.php" method="post" name="links">
    Search:
    <input type="text" Name="T1" />

    <select name="dropdown" value="options">
    <option value"index.php">Choose an option</option>
    <option value="title">Film Title</option>
    <option value="cast">Cast</option>
    <option value="director">Director</option>
    <option value="newreleases">New Releases</option>
    </SELECT>

    <INPUT type="submit" value="search">

    </form>
    the page that that drop downs will link to are;

    index.php
    titlesearch.php
    castsearch.php
    directorsearch.php
    newreleases.php

    any help will be great

    Thankyou, sian!

  2. #2
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    What you have should work fine.
    What's the code at results.php?

    Also note that you can validate the selection first with javascript, but that's a question for another forum.
    Oh Lord, please help me be the person my dog thinks I am.

  3. #3
    Join Date
    Nov 2006
    Posts
    22
    mm there isnt any, thats where im stuck.

    i dont know how to link the selected option in the drop down to the relevant page and the users input in the text box.

    thanks

  4. #4
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    ok. using different pages for each option, your results.php page needs to collect the values from your form, and will look something like this...

    First, just to make less work for yourself, I'd change my form so that the target pages were each listed as the values in the dropdown, like this...
    HTML Code:
    <select name="dropdown" value="options">
    <option value"index.php">Choose an option</option>
    <option value="titlesearch.php">Film Title</option>
    <option value="castsearch.php">Cast</option>
    <option value="directorsearch.php">Director</option>
    <option value="newreleases.php">New Releases</option>
    </select>
    and results.php can then do things one of two ways...

    EITHER...
    read the incoming data in results.php and redirect the user to the appropriate page like this
    PHP Code:
    <php
    $text 
    $_REQUEST['T1'];
    $drop $_REQUEST['dropdown'];
    header("Location: http://www.yoursite.tld/".$drop."?T1=".$text);
    ?> 
    Note that you've sent your T1 value along to the required script as a GET. Therefore, instead of using $_POST[] or $_GET[] to receive submitted values, use $_REQUEST[], which reads both.

    Or, in other words, add this to the top of each of your other scripts (including index.php)
    PHP Code:
    $text $_REQUEST['T1']; 
    OR...
    read the incoming data and then include the appropriate search script. This would be my preferred option, and would be slightly quicker.

    PHP Code:
    <php
    global $text//makes $text available to the script you're about to include
    $root $_SERVER['DOCUMENT_ROOT'];//gets the path of your script
    $text $_REQUEST['T1'];
    $drop $_REQUEST['dropdown'];
    include(
    $root."/".$drop)
    ?> 
    With this option, you don't need to add any data collection, because it's already collected.

    In either option, if no selection has been made, you're at least returning T1 to index.php so that they don't need to type it again. change the input line to this...
    HTML Code:
    <input type="text" Name="T1" value="<?=$text?>" />
    I hope that helps.

    Cheers
    CTB
    Last edited by chestertb; 05-11-2007 at 06:08 PM.
    Oh Lord, please help me be the person my dog thinks I am.

  5. #5
    Join Date
    May 2007
    Posts
    3
    chestertb,

    A fellow student told me about this forum, and how helpful it was. After reading your reply to the posted question, I see what he was talking about. I just wanted to say thanks for the explanation, and examples.

    -marinaccio

  6. #6
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    Thanks for the feedback. There are others here who know way more about using php than I do, but I do know that when you're just starting out, sometimes a little explaination with an answer can be useful.

    Good luck with it.
    Oh Lord, please help me be the person my dog thinks I am.

  7. #7
    Join Date
    Nov 2006
    Posts
    22
    hey, thankyou for all of that help.

    im getting an error message though. heres the code i used (hope i did it right)


    index.php

    <?php
    $text = $_REQUEST['T1'];
    $drop = $_REQUEST['dropdown'];
    ?>


    <form action="results.php" method="post" name="links">
    Search:
    <input type="text" Name="T1" value="<?=$text?>" />

    <select name="dropdown" value="options">
    <option value"index">Choose an option</option>
    <option value="titlesearch">Film Title</option>
    <option value="castsearch">Cast</option>
    <option value="directorsearch">Director</option>
    <option value="newreleasessearch">New Releases</option>
    </SELECT>

    <INPUT type="submit" value="search">

    </form>

    results.php

    <?php

    $db = mysql_connect("localhost", "root", "");


    //connect to database
    $result = mysql_select_db("filmcity",$db);
    if ($result == false){
    echo "cannot connect to db";
    die (mysql_error());
    }

    $text = $_REQUEST['T1'];
    $drop = $_REQUEST['dropdown'];


    if ($drop = 'titlesearch')
    header("Location: http://localhost/filmcity/titlesearch.php?T1=".$row["FilmTitle"]."' ");

    ?>
    the error is;

    Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\filmcity\results.php:7) in C:\Program Files\xampp\htdocs\filmcity\results.php on line 24

    is there something i havent defined or done wrong?
    any help would be great

    thankyou

  8. #8
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    That error means something in the script has tried to send output to the user.

    A couple of questions...
    Is that the whole script?

    If yes...
    then why are you connecting to your database? It's not needed.
    And the only thing sent is the failure to connect error, so I'd try moving that error message so that you have an either/or, something like this

    PHP Code:
    <?php
    $db 
    mysql_connect("localhost""root""");

    //connect to database
    $result mysql_select_db("filmcity",$db); 
    if (
    $result != false)
    {

       
    $text $_REQUEST['T1']; 
       
    $drop $_REQUEST['dropdown'];

       if (
    $drop 'titlesearch')
       
    header("Location: http://localhost/filmcity/titlesearch.php?T1=".$row["FilmTitle"]."' ");

    }
    else
    {
      echo 
    "cannot connect to db";
      die (
    mysql_error());


    ?>
    If No...
    Then look for something else in your script that might be outputting to the user... like an error message or a stray bit of code.

    If no header has been sent, and you try and send something (that's not a header) to the user, php will send a header. Once the header's been sent, it's been sent.

    CTB
    Oh Lord, please help me be the person my dog thinks I am.

  9. #9
    Join Date
    Nov 2006
    Posts
    22
    its still the same, i tried the other code you gave me and it works fine but no matter what i enter into the input box to search for a title, it always comes up with no entires when using the query

    $result = mysql_query("SELECT * FROM films WHERE FilmTitle = '".$_POST['T1']."'",$db);
    if i change it to

    $result = mysql_query("SELECT * FROM films WHERE FilmTitle > '".$_POST['T1']."'",$db);
    it brings up every film entry in the database.

    thanks for helping me, i know im a pain, im still learning all of this

  10. #10
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    I should have picked this up in your code, but didn't because I was looking for something something else...

    Where does $row["FilmTitle"] come from?

    Your user has entered a film title that you have received as T1, and stored as $text.

    You now have to send that text to the next script in the chain through the header() function like this...
    header("Location: http://localhost/filmcity/titlesearch.php?T1=".$text);

    At the moment, you're not sending any value as T1.

    By the way, even if $row["FilmTitle"] was valid, the syntax for associative arrays should be $row['FilmTitle'] (note the single quotes).
    Oh Lord, please help me be the person my dog thinks I am.

  11. #11
    Join Date
    Nov 2006
    Posts
    22
    this it all of the code im using

    index.php

    <?php
    $text = $_REQUEST['T1'];
    $drop = $_REQUEST['dropdown'];
    ?>

    <form action="results10.php" method="post" name="links">
    Search:
    <input type="text" Name="T1" value="<?=$text?>" />

    <select name="dropdown" value="options">
    <option value"index.php">Choose an option</option>
    <option value="titlesearch.php">Film Title</option>
    <option value="castsearch.php">Cast</option>
    <option value="directorsearch.php">Director</option>
    <option value="newreleases.php">New Releases</option>
    </select>

    <INPUT type="submit" value="search">

    </form>
    results.php

    <?php
    $text = $_REQUEST['T1'];
    $drop = $_REQUEST['dropdown'];
    header("Location: http://localhost/filmcity/".$drop."?T1=".$text);
    ?>
    titlesearch.php

    <html>

    <body>

    <body>
    <?php
    $db = mysql_connect("localhost", "root", "");
    mysql_select_db("filmcity",$db);
    $result = mysql_query("SELECT * FROM films WHERE FilmTitle = '".$_POST['T1']."'",$db);
    echo "<table border=1>\n";
    echo "<tr><td>film title</td><td>cert.</td></tr>\n";
    while ($myrow = mysql_fetch_row($result)) {
    printf("<tr><td>%s</td><td>%s</td></tr>\n",
    $myrow[1],$myrow[2]);
    }
    echo "</table>\n";
    ?>

    </body>

    </html>
    I decided not to use the code i print previously as im not really sure what to do with it.

    the only thing wrong with that above is that it either returned all of the films or none of them.

    thanks chestertb

  12. #12
    Join Date
    Nov 2006
    Posts
    22
    filmtitle is a field in my database table btw.

  13. #13
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    Easy.

    You've used $_POST to get the title into your sql query, but you've acutally used the $_GET method to send it.

    Two ways to fix it.
    The first is to simply change your sql query to
    $result = mysql_query("SELECT * FROM films WHERE FilmTitle = '".$_GET['T1']."'",$db);
    The second (and my preferred), is to use $_REQUEST[] instead of $_GET or $_POST. It reads both, and while there are some little tricks to using it (like that it also reads cookies, so you have to be aware of your field names), it's far more efficient than trying to figure out which is which.

    Cheers
    CTB
    Oh Lord, please help me be the person my dog thinks I am.

  14. #14
    Join Date
    Nov 2006
    Posts
    22
    I realised that after Id posted it that id used post instead of request, it works fine now.

    thankyou so much for all you help, really means alot

  15. #15
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    Oh Lord, please help me be the person my dog thinks I am.

Thread Information

Users Browsing this Thread

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

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