Jump to content

[SOLVED] Selecting an item in a select box from $_POST variable


Asheeown

Recommended Posts

I am doing error reporting on a page, and it's a submit page for information.  There are two select drop down boxes, with hundreds of options in each.

 

Now if they choose one of those correctly but do not fill out a required field it will redirect them back and tell them "One of the required fields is not correctly filled in"

 

I don't want the user to have to search back through those boxes again, is their a more simple way of selecting the one that the user selected before using the value in the $_POST variable?

Are the lists static or created dynamically? If they are dynamic it's easy, for static it's a lot of copy paste. This is what I normally do:

 

<?php
  print "<select name='test'>";
  foreach($items as $key=>$val){
    $selected= ($key == $_POST['test']) ? ' SELECTED' : ''; //Sets to SELECTED if it matches POST
    print "<option value='{$key}'{$selected}>{$val}</option>";
  }
  print "</select>";
?>

 

Only going to post a few of the select options as to not load the screen up, here are the two, summarized.

 

<select name="Make" style="width: 90px">
<option value="542">Abarth</option>
<option value="457">AC</option>
<option value="58">Acura</option>
<option value="16">Alfa Romeo</option>
<option value="456">Allard</option>
<option value="609">Allstate</option>
<option value="486">Alpine</option>
<option value="501">Alvis</option>
<option value="66">American Motors</option>
<option value="44">AM General</option>
<option value="550">Amphicar</option>
</select>

<select name="CarModel" style="width: 90px">
<option value="973">100 (Car)</option>
<option value="2143">100 (Truck)</option>
<option value="4719">1000 (Car)</option>
<option value="6294">1000 (Medium Duty)</option>
<option value="6264">1000A</option>
<option value="2144">1000B</option>
<option value="2145">1000C</option>
<option value="2146">1000D</option>
</select>

 

 

EDIT: Rhodesa, they are all static :\ wasn't my choice

 

Unless you want to do a whole lot of cutting a pasting I would suggest you put the dropdown options in an array.

 

<?php
$makes = array(542 => "Abarth",
               457 => "AC",
               58 => "Acura",
               16 => "Alfa Romeo"
               );

echo "<select name=\"Make\" tyle=\"width: 90px\">\n";
foreach($makes as $key => $val){
$selected= ($key == @$_POST['test']) ? ' SELECTED' : ''; //The @ sign will suppress the error if it is not set. Sets to SELECTED if it matches POST
echo "<option value=\"$key\" />$val</option>\n";
}
echo "</select>\n";
?>

 

Otherwise you will have to do something like this for each one

<select name="Make" style="width: 90px">
<option value="542" <?php if(@$_POST['Make'] == "542"){ echo "selected"; } ?> >Abarth</option>
<option value="457" <?php if(@$_POST['Make'] == "457"){ echo "selected"; } ?> >AC</option>
<option value="58" <?php if(@$_POST['Make'] == "58"){ echo "selected"; } ?> >Acura</option>
<option value="16" <?php if(@$_POST['Make'] == "16"){ echo "selected"; } ?> >Alfa Romeo</option>

 

Ray

well, you can either make an array, and generate them dynamically:

<?php
  $makes = array(
    542 => 'Abarth',
    457 => 'AC',
    58 => 'Acura',
    //etc
  );
  print "<select name=\"Makes\" style=\"width:90px;\">";
  foreach($makes as $key=>$val){
    $selected= ($key == $_POST['test']) ? ' SELECTED' : ''; //Sets to SELECTED if it matches POST
    print "<option value=\"{$key}\"{$selected}>{$val}</option>";
  }
  print "</select>";
?>

 

or do lot's of copy/paste:

<select name="Make" style="width: 90px">
<option value="542"<?php if($_POST['Make'] == '542') print " SELECTED"; ?>>Abarth</option>
<option value="457"<?php if($_POST['Make'] == '457') print " SELECTED"; ?>>AC</option>
<option value="58"<?php if($_POST['Make'] == '58') print " SELECTED"; ?>>Acura</option>
</select>

 

or use some javascript:

<script type="text/javascript">
function selectOption ( ele_name, value ) {
  var ele = document.forms[0][ele_name];
  if(!ele) return false;
  for(var i = 0;i < ele.options.length;i++){
    if(ele.options[i].value == value){
      ele.selectedIndex = i;
      return true;
    }
  }
  return false;
}
<select name="Make" style="width: 90px">
<option value="542">Abarth</option>
<option value="457">AC</option>
<option value="58">Acura</option>
<option value="16">Alfa Romeo</option>
<option value="456">Allard</option>
<option value="609">Allstate</option>
<option value="486">Alpine</option>
<option value="501">Alvis</option>
<option value="66">American Motors</option>
<option value="44">AM General</option>
<option value="550">Amphicar</option>
</select>
<?php
  if(isset($_POST['Make']))
    print "<script type=\"text/javascript\">selectOption('Make','{$_POST['Make']}');</script>";
?>

<select name="CarModel" style="width: 90px">
<option value="973">100 (Car)</option>
<option value="2143">100 (Truck)</option>
<option value="4719">1000 (Car)</option>
<option value="6294">1000 (Medium Duty)</option>
<option value="6264">1000A</option>
<option value="2144">1000B</option>
<option value="2145">1000C</option>
<option value="2146">1000D</option>
</select>
<?php
  if(isset($_POST['CarModel']))
    print "<script type=\"text/javascript\">selectOption('CarModel','{$_POST['CarModel']}');</script>";
?>

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.