laurenhouse Posted February 1, 2010 Share Posted February 1, 2010 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!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/190569-help-with-form/ Share on other sites More sharing options...
wildteen88 Posted February 1, 2010 Share Posted February 1, 2010 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/190569-help-with-form/#findComment-1005119 Share on other sites More sharing options...
laurenhouse Posted February 1, 2010 Author Share Posted February 1, 2010 Thank you, but I got an error message : Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/f/l/i/flipthebirds/html/congress-result.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/190569-help-with-form/#findComment-1005150 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.