Search code examples
phpjavascriptdata-mapping

Best way to track database table/column for html form control


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?


Solution

  • 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);
    }