Jump to content

Select box question! very simple please help me!


pjc2003

Recommended Posts

hi ive got a select box that gives a value to variable $location_id

<select name="location_id">
      <option =1 <?php if ($location_id = 1) echo ' selected '?> >Location A</option>
      <option =2 <?php if ($location_id = 2) echo ' selected '?> >Location B</option>
      <option =3 <?php if ($location_id  = 3) echo ' selected '?> >Location C</option>
      <option =4 <?php if ($location_id  = 4) echo ' selected '?> >Location D</option>
</select>


Ok what i want to do is have the select box display the option "location x" etc ... all the way from A - Z which I can do fine  BUT  I want the location_id variable to hold its corresponding number! But no matter what I try the variable stores the value of what is actually displayed in the select box...


Please help me!

pj
The comparison operater is "==" not "=".

You also need to write correct HTML if you expect your forms to work:
[code]<?php
$location_id = 0;
if(isset($_POST['submit'])) {
        echo '<pre>' . print_r($_POST,true) . '</pre>';
        $location_id = $_POST['location_id'];
}
?>
<form method="post">
<select name="location_id">
      <option value="1" <?php if ($location_id == 1) echo ' selected '?> >Location A</option>
      <option value="2" <?php if ($location_id == 2) echo ' selected '?> >Location B</option>
      <option value="3" <?php if ($location_id == 3) echo ' selected '?> >Location C</option>
      <option value="4" <?php if ($location_id == 4) echo ' selected '?> >Location D</option>
</select><br>
<input name="submit" value="Do Test" type="submit">
</form>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.