views:

30

answers:

1

I've been trying to find the best way to do this for a while now but cannot figure it out.

I have a simple dropdown which auto populates based on an SQL query. When you press "search" I need it to go to a page with the url extension ?id=x where x is the id of the option they selected.

$locations = $wpdb->get_results( $wpdb->prepare(
    "SELECT * FROM wp_places_index WHERE visible='Y' ORDER BY place_name ASC") );
?>
<form id="find-buses-form" method="post" action="places_index.php">
<select class="default-value" type="text" name="from">
<option>Please select...</option>
<?php 
    foreach($locations as $location)
    {
    echo "<option>".$location->place_name."</option>";
  // maybe this?  echo "<input type=\"hidden\" name=\"id\">".$location->id."</input>";
    }
?>

</select>
<input type="submit" name="submit" value="Show me" />   
</form>

I think I may need to make it go to an external page which uses $_POST to pull that hidden field but I'd rather do it on one page.

I've achieved this before out of wordpress using something like this:

while ($row = mysql_fetch_array($result))
            {
                    $picture = $row['Image'];
                    if($picture == ""){
                    $picture= "thumbnails/no-image-small.png";
            }
                    $product_ID = $row["ProductID"];

But wordpress does not like the mysql_fetch_array :( Any advise?

+3  A: 

You need to put whatever in a value attribute of your option tag. For example:

<?php
$locations = $wpdb->get_results( $wpdb->prepare(
    "SELECT * FROM wp_places_index WHERE visible='Y' ORDER BY place_name ASC") );
?>
<form action="places_index.php" method="get">
  <select name="x">
<?php foreach ($locations as $location): ?>
    <option value="<?php echo $location->id; ?>"><?php echo $location->place_name; ?></option>
<?php endforeach; ?>
  </select>
</form>

When submitted, the form will go to http://example.com/places_index.php?x=1 presuming the name of your select is x and the option selected has 1 in its value attribute.

Hope this helps.

Martin Bean
I thought it might have something to do wiht the value, but I was not sure how to set it up. thanks a lot man
Adam
No problem. Glad to have helped!
Martin Bean