0

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

6
  • Is this for a single record?, Send your the "id" to query? Commented Sep 11, 2010 at 13:44
  • yeah there is an id also, which is not editable with those fields the whole page is for a single record Commented Sep 11, 2010 at 13:47
  • can you show a current output of "handle.php" for type "get"? Commented Sep 11, 2010 at 13:48
  • ok, but for "handle.php" with type "get", send your the id query? Commented Sep 11, 2010 at 13:50
  • let's say its a person with id,name,familyname , so after reciving his id=123 it will show : 123,misha,grisha Commented Sep 11, 2010 at 13:50

1 Answer 1

0

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>
0

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.