views:

61

answers:

5

I am using php to build a "change classifieds" page right now.

I use Mysql as a db.

Currently I use PHP to fetch all mysql information about the classified, and then I output it like this:

  $table.="
   <select name='year' id='year'>
     <option id='2000' value='2000'>2000</option>
     <option id='2001' value='2001'>2001</option>
     <option id='2002' value='2002'>2002</option>
   </select>";

   echo $table;

I have a picture upload tool which submits the page to itself, and at the same time uploads the picture, and shows it at the bottom of the page. The problem is, whenever this is done, the user must fill in all information again, because they are "forgotten".

I know about input type="text" where you simply can do something like this:

   <input type="text" name="text" value="<?php echo $_POST['text'];?>">

but what about selects? Radios? etc?

What should I do here?

Thanks

A: 

You can conditionnally echo selected in PHP, depending on the value to select.

tsimbalar
How exactly? Got a good example?
Camran
check the other answers, they are better than mine ;-)
tsimbalar
+3  A: 

You can set the selected attribute to "selected" to make it the default/current selection:

<option id="2000" value="2000" selected="selected">2000</option>

php example:

$options = array( '2000' => '2000', '2001' => '2001', '2002' => '2002' );
$selected = '2001';
$select = '<select name="year" id="year">';

foreach ( $options as $key => $value )
{
    $select .= '  <option value="' . $key . ( $key == $selected ? '" selected="selected">' : '">' ) . $value . '</option>';
}

$select .= '</select>';
poke
How does my script know which value to set to selected then? That is the question...
Camran
Updated my answer to contain some php code.. That also constructs the select options dynamically.
poke
Don't forget to echo `$select`.
Ramon Marco Navarro
+2  A: 
<option 
  id='2000'
  value='2000'
  <?php if(isset($year) && $year === '2000') echo 'selected="selected"'?>
>2000</option>

Where $year contains the year from wherever. This assumes $year is a string. If $year is an integer change the condition to:

  if(isset($year) && $year === 2000)

For radio buttons and checkboxes just replace selected="selected" with checked="checked".

Ramon Marco Navarro
what if $year is INTEGER?
Camran
Ramon Marco Navarro
+2  A: 

For selects, it is a bit more intense you need to loop through the data and check if the value equals the selected value (at least that is the easiest to do). But you could apply a similar technique as seen with the checkbox / radio.

<input type="radio" name="radio1" value="1" <?php echo (!empty($_POST['radio1']))?'checked':''?>> 
<input type="checkbox" name="chkbox1" value="1" <?php echo (!empty($_POST['chkbox1']))?'checked':''?>>

Since you are creating the table PHP side here is the code:

  $years = array('2000', '2001', '2002');

  $table .= "<select name='year' id='year'>";
  foreach ($years as $year) {
      $table .= "<option id='" . $year . "' value='" . $year . "' " . 
            (($_POST['year'] == $year)?'selected':'') . ">" . $year . "</option>\n";
  }

  $table.="</select>";

Should get you on the right track for select statements. The ? and : make up the ternary operator which is a shortened if / else statement.

Brad F Jacobs
Thanks, something like this I needed. Do you know if there is a similiar code for the SELECT at client side? (like the example on checkboxes and radios you put there) ?
Camran
There is no client side with php. You simply need to echo out the correct code at the correct location. That's what you do with the inline style for the input types as well..
poke
To do this on the client side you would need to use Javascript and tell javascript what should be selected, so the javascript file would need the variable implanted in some way or form.
Brad F Jacobs
A: 
<?php

$selected[$_GET['year']] = "selected='selected'";

$table.="
 <select name='year' id='year'>
   <option id='2000' value='2000' " . $selected['2000'] . ">2000</option>
   <option id='2001' value='2001' " . $selected['2001'] . ">2001</option>
   <option id='2002' value='2002' " . $selected['2002'] . ">2002</option>
 </select>";

echo $table;
?>
Alexio