Jump to content

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


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.

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.