Jump to content

[SOLVED] default values to form fields set by php variables


jib

Recommended Posts

So I hit another brick wall, I googled, didn't find the answer so here goes,

 

I am using variables to fill form fields. The text boxes/ text areas were simple. But what do you do for radio buttons and drop down menu's?

 

I want to set the the default value for these fields to value of the variable. Basically it's just an update entry form so if they clicked a radio button when they submitted that button will be check and if they picked a certain drop box value it will have that one selected.

 

any ideas?

<td>Source:</td>

    <td><input name="Source" type="radio" value="Recruiter" />

      Recruiter</td>

  </tr>

  <tr>

    <td> </td>

    <td><input type="radio" name="Source" value="School" />

      School</td>

  </tr>

 

for the radio, now if $source = recruiter i want recruiter to be default checked

 

<select name="School_Type" id="School_Type">

      <option>Private Academy</option>

      <option>Public School</option>

      <option>Camp</option>

      <option>English Village</option>

      <option>University</option>

      <option>Other</option>

    </select>

 

for drop box if $School_Type = Camp I want camp to be the default value etc

then I will do like this

 

$School_Type=$_POST['School_Type'];
$School_Type_aray[]="Private Academy";
$School_Type_aray[]="Public School";
$School_Type_aray[]="Camp";
$School_Type_aray[]="English Village";
$School_Type_aray[]="University";
$School_Type_aray[]="Other";
echo "<select name='School_Type' id='School_Type'>";

for($i=0;$i<count($School_Type_aray);$i++){
     $selected="";
     if($School_Type_aray[$i]==$School_Type){
          $selected="selected";
     }
     echo "<option value=".$School_Type_aray[$i]." ".$selected.">".$School_Type_aray[$i]."</option>";
}
echo "</select>";

 

same kind of thing for radio.

 

** Dont use same name for textbox and radio !! :)

first place a value... then compare an if()... if it hits... echo selected.

 

<?php $select = 2; ?>

<select name="School_Type" id="School_Type">
     <option value=1<?php if(1 == $select) echo " selected";?>>Private Academy</option>
     <option value=2<?php if(2 == $select) echo " selected";?>>Public School</option>
     <option value=3<?php if(3 == $select) echo " selected";?>>Camp</option>
     <option value=4<?php if(4 == $select) echo " selected";?>>English Village</option>
     <option value=5<?php if(5 == $select) echo " selected";?>>University</option>
     <option value=6<?php if(6 == $select) echo " selected";?>>Other</option>
</select>

 

not really the best solution but would do the trick.

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.