I'm using php to construct a "change classifieds" page at this time.

I personally use Mysql like a db.

Presently I personally use PHP to fetch all mysql details about the classified, after which I output it such as this:

   <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>

   echo $table;

I've got a picture upload tool which submits the page to itself, and simultaneously uploads the image, and shows it at the end from the page. The issue is, whenever this is accomplished, the consumer must fill in most information again, since they're "forgotten".

I understand about input type="text" in which you simply can perform something similar to this:

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

but how about chooses? Radios? etc?

What must i do here?


You are able to set the selected attribute to "selected" to really 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>';
  <?php if(isset($year) && $year === '2000') echo 'selected="selected"'?>

Where $year consists of the entire year no matter where. This assumes $year is really a string. If $year is definitely an integer alter the condition to:

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

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

For chooses, it's a little more intense you have to loop with the data and appearance when the value equals the chosen value (a minimum of that's the simplest to complete). However, you could use a similar technique as seen using 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':''?>>

As you are creating the table PHP side this is actually 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";


Should enable you to get on course for choose claims. The ? and : from the ternary operator the industry reduced if / else statement.

You are able to conditionnally echo selected in PHP, with respect to the value to choose.


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

 <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>

echo $table;