views:

41

answers:

1

question relates to PHP and Javascript

for now every table has a form with input tags that each of them has id="field_from_table"

GLOBAL_TABLE=name of that table
GLOBAL_FIELDS=name of fields in that table
GLOBAL_ID=ID value for table.

every field also have a label for im. some of the fields are not text.

I want to get/set those input tags.

now it's design like this:

    <script>
    function get()
    {
    id=GLOBAL_ID
    $.post("handle.php",{type:"get",tablename:GLOBAL_TABLE,fields:GLOBAL_FIELDS,id:GLOBAL_ID),function(data)
    {
       fieldValues=/*javascript explode data*/;
       foreach(/*array of fields as idx=>fieldName*/)
           {$("#"+fieldName)=fieldValues[idx];}
    }
    function set()
    {
    /*for each GLOBAL_FIELDS as idx=>fieldName
      values[fieldName]=$("#"+fieldName).val;
    */

    valuesI=/*implode values*/;
    $.post("handle.php",{type:"set",tablename:GLOBAL_TABLE,fields:GLOBAL_FIELDS,values:valuesI),
      function(data)
        {
        if (data!=null) alert ("error");
        }
    }
</script>

handle.php updates the information into table and fields. or selects and outputs CSV.

problem is that i need to output a field list from php so javascript can use it and i don't think that using javascript that way is a good idea

is there a better design.

a good answer or an advice is most appreciated.

arye

A: 

try this:

<script>

    function get()
    {

        $.post("handle.php",{type:"get",tablename:GLOBAL_TABLE,fields:GLOBAL_FIELDS),function(data)
            {
               var fieldValues = data.split(','), 
                   fieldNames = GLOBAL_FIELDS.split(',');

               for(var ind = 0; ind < fieldNames.length; ind++) {
                    $("#"+fieldName[ind])=fieldValues[ind];
               }
            }
        }); 
    }

    function set()
    {
         var fieldValues = "", 
             fieldNames = GLOBAL_FIELDS.split(',');

         for(var ind = 0; ind < fieldNames.length; ind++) {
                valuesI += ((fieldValues.length == 0) ? "" : ",") + unescape($("#"+fieldName[ind]).val());
         }

        $.post("handle.php",{type:"set",tablename:GLOBAL_TABLE,fields:GLOBAL_FIELDS,values:valuesI), function(data) 
            {
            if (data!=null) 
                alert ("error");
          }
        }); 
    }
</script>
andres descalzo
thanks for correcting it.
shevski