Forums

Populating form Dropdown

adcom 08 Dec, 2010
Hi,

I am using a chronoform where i have a drop down with OPtion Groups and OPtion Labels in the Drop down as a subject field for Example : We Have OPtion Group FRUITS having Red Apple,Yellow Banana,Green Grapes as OPtions. We want when a person clicks from a certain page gets in a prefilled subject Red Apple e.g i am using the following URL to call the form
http://www.abc.com/contactus.html?subject=Red Apple but the form appears with no subject selected.
Can you please help with the same.

Regards
ADCOM
GreyHead 08 Dec, 2010
Hi ADCOM,

As you can see, there is a problem with spaces in URLs - the values have to be urlencoded and then decoded to work correctly.

I'm not clear quite what you want to do with the value from the URL - is it just to display it as text? The simplest code is:
<?php
$subject = JRequest::getString('subject', '', 'get');
echo $subject;
?>


Bob
adcom 08 Dec, 2010
Thanks Bob,

For replying to my query.
I just want the Subject drop down to come with a preselected subject which we are mentioning in the URL when a person clicks on the form URL. It means when he reaches the FORM he doesnt have to select the subject Drop Down Value which is already preselected in the drop down when form appears

Best Regards
ADCOM
GreyHead 08 Dec, 2010
Hi ADCOM,

And what is he code you are using to generate the drop-down? Is it plain HTML or created with PHP? Can you post it here please

Bob
adcom 08 Dec, 2010
HI Bob,

Thanks for replying i have added the code in the file attachement, presently i am using the HTTP referrer for a label, but since i want a particular label to be called from more than one URL i have to create duplicate code for the same with the new URL so a new label is created in the form, thats why i am looking for the URL Links where i can call the Value in Subject Variable from the contact form URL such as http://www.abc.co.uk/contactus.html?subject=First Aid Trainer. so that i dont have to add more HTTP referrals for the same course and i can create a new link for same option with URL link.

Best Regards
ADCOM
GreyHead 09 Dec, 2010
Hi adcom,

Here's one way to generate the select options. I've used an array with short-codes - like 'ffawta' - to be passed in the URL. You could use the longer name urlencoded but generally using short codes and looking them up as needed is simpler in the long term.
<select name="subject" onChange="showSelection(this)">
<option selected>Please Select the Course</option>
<optgroup label="Trainer Courses">
<?php
$subject_array = array (
'ffawta' => 'First Aid At Work Trainer/Assessor',
'mhta' => 'Manual Handling Trainer/Assessor',
'ipht' => 'IOSH Patient Handling Trainer',
'adta' => 'AED Defib Trainer/Assessor',
'fst' => 'Fire Safety Trainer',
'fsra' => 'Fire Safety Risk Assessor',
'hasta' => 'Health & Safety Trainer/Assessor',
'cpta' => 'CIEH Professional Trainer Award',
'cat' => 'COSHH Assessor/Trainer',
'cra' => 'COSHH Risk Assessor',
'ana' => 'A1 NVQ Assessor',
'vniv' => 'V1 NVQ Internal Verifier',
'dvat' => 'DSE/VDU Assessor/Trainer',
'dra' => 'DSE Risk Assessor',
'ht' => 'Healthcare Trainer',
'lt' => 'Lifeguard Trainer'
);
$subject = JRequest::getString('subject, '', 'get');
foreach ( $subject_array as $k => $v ) {
  $s= '';
  if ( $subject == $k ) {
    $s = "selected='selected'";
  }
  echo "<option label='$v' value='$v' $s>$v</option>";
}
?>
</optgroup>
</select>


Note that I would also use the short codes as the option values and either put the array into an external file to be included wherever it is needed; or, more likely, look it up from a database table with a list of course names.

Bob
This topic is locked and no more replies can be posted.