0

In my web-application i have a form which contains about 25 fields. Users will create this form and form fields value will transfer to database tables. But i also need an editing function for this form. People will push button, form will load and i need form fields values to load from database. I know that i can load data to form field with jquery load function:

$('#text_field').load('text_field_value.php');

But when i have 25 fields, then i need to do 25 connections to database (1 connection per file), so my form loading very slow.

Is there any solution, to speed up the process?

Thanks in advance.

2
  • 2
    You could fetch all the values for the row as a JSON array for example? Commented Aug 31, 2010 at 9:34
  • i found easier solution: I just create a 1 database connection in form file and put values to form fields and then load this form to the user. So, no load, no getJSON, no .post, no .ajax. Very simple. Why i don't think about it earlier? Commented Aug 31, 2010 at 18:00

1 Answer 1

1

Perhaps you should load the field values using one ajax request so that you are only using one connection to the database and then as Pekka said return a JSON Array. Then using jQuery go through the JSON Array putting the values into the correct fields.

So use .getJSON/.post/.ajax instead of .load

EDIT: Example JSON Array, with objects for each field

[
    {
        'field'      : '#field1',
        'fieldType'  : ':text',
        'fieldValue' : 'Test Value'
    },
    {
        'field'      : '[name=field2]',
        'fieldType'  : ':radio',
        'fieldValue' : '#item1'
    },
    {
        'field'      : '#field3',
        'fieldType'  : 'select',
        'fieldValue' : 5
    },
    {
        'field'      : '#field4',
        'fieldType'  : ':checkbox',
        'fieldValue' : true
    }
]

You can then loop through the array as follows:

for(i in array)
{
    /***
     * access each object as follows:
     * array[i].field
     */
    .....
}
Sign up to request clarification or add additional context in comments.

4 Comments

can i put all info to standard array, not json?
If you want to interact with the array I think you would need to return a JSON array. I'll add an example of how I would make the JSON array.
i'm not familiar with json requests. Can you add a .getJSON for that example? Thank you
I'm a bit busy at the moment but will add an example when I get the chance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.