Jump to content

Recommended Posts

I'm new to PHP and need some help with a drop down list.

 

I have a form on my page that generates a drop down list based on a table in my database. The purpose of this is so users can select their county, and the result will be the names and email addresses of who represents that county.(This is in another table in the db)

 

The drop down options are counties in my state, and the option value is the name of the county.

Here's what my code looks like (mysql connection is made in the header):

 

<form name="congressman" id="congressman" method="post" action="congress-result.php">

                <select name="cty" id="cty">

              <?php

  $query1 = "SELECT county FROM counties";

  $result = mysql_query($query1);

 

  if(!$result) die("Database access failed: " . mysql_error());

 

  while($row = mysql_fetch_array($result))

  {

$county=$row['county'];

echo "<option value=". $county.">" . $county . "</option>";

}

?>

            </select>

            <input type="submit" value="Find Your Congressmen" />

            </form>

 

This generates the counties in a drop down nicely. When I click submit, I'd like the next page to generate the names and addresses based on the drop down selection. The query on the next page is ("SELECT * FROM representatives WHERE serves LIKE %(insert variable here, which I don't know what it should be, help!)%")

 

I'm having an extremely hard time trying to figure this out, so any help is much appreciated!! Thanks in advance!!!!!

Link to comment
https://forums.phpfreaks.com/topic/190569-help-with-form/
Share on other sites

To get the selected county from your drop down menu you'd use $_POST['cty']. Example

 

if(isset($_POST['cty']))
{
    $county = mysql_real_escape_string($_POST['cty']);
    $query = "SELECT * FROM representatives WHERE serves LIKE %($county)%";
    $result = mysql_query($query);

    while($row = mysql_fetch_assoc($result))
    {
         // display results from query
    }
}

Link to comment
https://forums.phpfreaks.com/topic/190569-help-with-form/#findComment-1005119
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.