Jump to content

Populate from Database info


Sbos

Recommended Posts

Here's what I'm trying to do:

 

populate a field from the database

(this I know how to do)

 

from there, depending on what you choose there is an array that needs to be associated with that.

 

so say they choose frame from my dropdown, I need a new field to be populated from the database with all the possible choices with the tag of frame.

 

and then again when they choose a frame type all the appropriate information about that frame gets filled in.

 

I thought of Javascript but that just doesn't seem like a rock solid idea, be a lot of javascript code in the source that might not be good to have in there if I used PHP to make the javascript code.  Any ideas?

Link to comment
Share on other sites

You have three choices. Two of them involve some level of JavaScript (Java != JavaScript). I like to use JavaScript solution when I can because they are faster, more user friendly, and *can* utilize less resources on the serve. However, there is a drawback in that there are a small percentage of users who do not have JS enabled on their browser. So, a decision must be made to determine if the possibility that some users will be unable to use the functionaliy or have a less rich experience must be made. For a personal site I would say use JS. For a buiess site the product owner should have made that deision.

 

Here are the three methods I would consider for this:

 

1) JavaScript only:

Use the PHP to create the appropriate arrays in the JavaScript code. Then use an onchange trigger on the primary field to populate the secondary field. The advantageous are that this will be very fast for the user and is relatively easy to develop. The only disadvantage is that it will be totally unusable by the user. However, you should take into account the total “size” of the arrays needed. If you have a primary list of states and then have secondary lists which may have hundreds of offices, then this may create to great of an overhead on the client

 

2) AJAX:

A combination of JavaScript and PHP. Basically you would still use JavaScript to fire an onchange event of the primary field. But then the JavaScript would call a PHP page to get only the new list items for the secondary list and return it back to the JavaScript which would populate the field. The advantageous are that this can handle large data sets more efficiently as only the needed data is being processed by the client. The disadvantageous are that the user must still have JavaScript enabled and there can be a slight delay. I have seen similar implementations where there are bugs when the user tries to change the primary field too quickly (i.e. the change the value before the secondary list has been fully repopulated). These can be easily overcome by adding the proper handling such as disabling the fields until the secondary list has been fully populated.

 

3) PHP Only:

This involves having the user submit the page whenever they make a change to the primary field. This is a “bullet proof” solution because it doesn’t require any JavaScript support from the browser. However, the user experience is not as “rich” in my opinion. If the user was to change the primary field several times it could be tedious.

Unless non-JavaScript browsers must be supported I typically go with options 1 or 2 based upon the amount of data.

 

Link to comment
Share on other sites

1) Java isn't Javascript =P

 

 

2) That being said - Javascript is a great solution to your problem.  Most browsers are very Javascript-friendly nowadays, and the interpreters in them are solid.  Everybody has been talking Ajax recently - and in order for Ajax to work, Javascript *must* be enabled.

 

Take a look at http://www.tizag.com/ajaxTutorial/ to get your feet wet.

 

Also, you will want to take a look at DOM Scripting (Document Object Model)  Some google searches will get you the results you're looking for.

Link to comment
Share on other sites

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.