PRE-POPULATE A PULL DOWN FORM FIELD FROM THE VALUES IN A MASTER LIST - Jun 3rd, 2014


I had created a multi-record master list section called master_list with a field called title. I wanted to pre-populate
the options values in a form from the values in the title field of the records in that section.

At the top of my page I insert a load records call for the master_list table


list($master_listRecords, $master_listMetaData) = getRecords(array(
'tableName' => 'master_list',
));


In the body, before the form, I created an array out of the title field values in each record.


<?php
$names = array();
foreach (
$master_listRecords as $record){
$names[$record['title']]=$record['title'];
}
?>


In the form I inserted the following to loop through the array and populate the pull down field:


<label for='exhibition_name'><span class="my_class">Exhibition Name:</span>
</label>
<select type="text" id="exhibition_name" name="exhibition_name">
<option value=""><span class="body-text-bold">Select an Exhibition Name</span></option>

<?php foreach($names as $name): ?>
<option value="<?php echo $name;?>"><?php echo $name;?></option>
<?php endforeach?>
</select>


Here’s another approach using the record number as the option value for a record in a table called
‘email_signup_location’ that contains a field called ‘location’.

The Javascript retains the selected value in the form after submission so that if there are any errors thrown, the pull
down does not need to be manually repopulated.


<select type="text" id="source" name="source" width="200" style="width: 200px" >
<option value="" >Your Location</option>
<?php foreach(mysql_select("email_signup_location") as $email_signup_locationRecord): ?>
<option value="<?php echo $email_signup_locationRecord['num'];?>"><?php echo
$email_signup_locationRecord['location'];?></option>
<?php endforeach?>
</select>
<script type="text/javascript">
document.getElementById('source').value = "<?php echo $_POST['source'];?>";
</script>




The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service