views:

82

answers:

3

I am working on a php program that requires javascript on every form control. Once the onblur event takes place the value will automatically be sent to the server to update the database.

I have possibly 50 form controls spread over 5 tabs so I don't want to hardcode the information on the php file.

I could co-opt an idea from HTML5 and create new properties for this such as

data-table='user' data-column='firstname'

for every item, and then javascript could pull those values out and send them to the php file.

I don't know if there is a better way to manage the mapping between these form controls and the several tables/columns?

+1  A: 

Hi,

Actually I think that's the best option you have. If you use jquery, you can do something like this :

$("input").blur(function () {
    var data-table = $(this).attr('data-table');
    var data-column = $(this).attr('data-column');

    $.ajax({
    type: 'post',
    url: your_url,
    data: your_get_data
    });
});
yoda
I was hoping for a better solution, as it seems like the best option to me, but, it is nice to sometimes see if I am missing something. Thanx for the response. <g>
James Black
Oh, one thing, I would not send this as a GET request, mainly because each GET request tends to be stored in a log file, but POST isn't, and no point filling up log files needlessly. I limit get requests a great deal, to where it makes sense.
James Black
Yeah, I forgott to change the get to post actually, you're right.
yoda
+1  A: 

HTML

<input id='data-base*data-colum' onblur='preupdate(this);' value='' />

Javascript

function preupdate(el){
   var idData = el.getAttribute('id').split('*');
   var dataBase = idData[0];
   var dataColum = idData[1];

   update(el.value, dataBase, dataColum);
}
andres descalzo
I like this idea of using the id field. I didn't think about putting both the table and column in it. The simplicity is great. Thank you.
James Black
I have implemented this for the updates and inserts and though I had to make some changes, encoding where columns and values, it worked out very well.
James Black
A: 

I'd use PHP to generate a data structure mapping the controls (hopefully they have unique names/ids) to the db info. Then I'd spit this out in JSON as inline javascript in .

Then your on_blur attributes on all your fields can be the same (or each pass a unique id for that field) and call a javascript function that looks up the db info from the data structure.

JasonWoof
This could be a nice solution for small databases, but if it begins to get large then it can start to be difficult to send the data back and forth, but, for a small amount of data this may be a good solution. Thank you.
James Black
Huh? Maybe you don't understand what I suggested. I'm thinking of having php generate a string like this: "field_meta = ['email': ['accounts','email'], 'description': ['profile','text']];" and put that in a <script> tag. Then your javascript can get the table like so field_meta[field_name][0] and the column like so: field_meta[field_name][1]. So we're talking probably 20-50 characters per field, all easily accessible as a hash.
JasonWoof
Did you think I meant to pass the entire structure of your entire database perhaps? I was thinking of just passing the table/column data for the fields that are actually on the form.
JasonWoof