I have a select field currently being populated using a simple bit of PHP for looping. The purpose of this field is to make completion of the form easier and quicker. By allowing the user to select presets that are available in the select field.
When an option is selected in the field a JavaScript function will be activated , which will populate the other fields in the form with data associated to that option.
Example situation what the JavaScript will do:
Please select an option - {Off peak tariff, Texter Package, GPRS package, Custom}
If user selects package that is not equal to Custom
Then insert values into fields {Voice allowance, SMS allowance, Internet allowance, Voicemail allowance}
Here is a screenshot of the form and illustration of what I am trying to do:

Currently I have my options populated purely with PHP like so.
//prints out option list for select field
foreach ($result as $array)
echo '<option value="'. $array.' ">'. $array. '"</option>';
This works fine at the moment however I have just realised I am going to make my life difficult at a later stage of development for the form as I am going to be using Javascript to manage that field, so it makes sense for the whole field to be done in JavaScript.
This means the JavaScript will :
- Load data from JSON that contains data for the select field and its associated data that will go in other fields.
- Populate the select field with data from JSON.
- Monitor the field for onchange.
- Effect other fields in form
As this is all rather new territory for me, I was hoping you guys could point me in the right direction how to:
- Pass data to JavaScript in the best possible way, do I have seperate arrays (one for pull down , one for other field values) or would an object be better to store everything and just pass that through JSON? ?
- How do I handle the data passed to JavaScript from PHP? Is there an equivelant to foreach in JavaScript to handle an array?
- Should I do all the preparation on PHP side or let JavaScript just handle the raw data?
Any examples of code or similar projects would be really welcome! :)
As you may be able to tell I have an idea in my head how I want to achieve this but I do not know if this is the best way.
Thanks for your time
<select>options in PHP was going to make your life difficult. What exactly makes you think that?