Topic: PHP script to select state

I have a database table called contacts with a table in it called addresses. The fields in the addresses table are firstname, lastname, address, city, state, zip, areacode and phonenumber. (The state field is a 2 character text field.) I want to ask the user for a state and then I want to display all the records for that state only. Can anyone help me?

Thanks,

Andrew

Thumbs up

Re: PHP script to select state

You can place a select box with all the available states in contacts and populate it in a select box.  Then after that based on the selected values pass query to the database

<select name="state">
<?php
$sql   =  "SELECT DISTINCT ( state ) FROM contacts ORDER BY state";
$res = mysql_query( $sql ) or die( mysql_error());
while( $row = mysql_fetch_object( $res ) ){
           echo '<option = '". $row->state ."'>'. $row->state .'</option>';
}
?>
</select>
<input type="submit" name="submit" value="search">

Then in the landing page do the following code

<?php 
if( isset( $_POST['state'] ) ){
       $sql = "SELECT * FROM contacts WHERE state = "".  $_POST['state'] ."'";
       //Remaining Operations
}

Thumbs up