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
Link to comment
Share on other sites

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
Link to comment
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.