[SOLVED] Button to load data into form

[SOLVED] Button to load data into form

I have a table with a load of stuff from a database. At the end of each row I have an edit and delete button. The delete button works magically, however I am trying to get the edit button to load data into a form above the table to be edited.
This is what I have so far.

  1. $(".editbtn").click(function () {
  2.         if ($(this).attr("src") == "images/edit.png") {
  3.             $("#add").slideUp("slow");
  4.             $("#addform").slideUp("slow");
  5.             id = $(this).parents("tr:first").attr("id");
  6.             //Remove all selected classes
  7.             $("tr").removeClass('selected');
  8.             //Reset images
  9.             $(".editbtn").attr("src","images/edit.png");
  10.             $("#"+id).addClass('selected');
  11.             $(this).attr("src","images/load.gif");
  12.             //Load data into form
  13.             var op = $(this);
  14.             $.ajax({
  15.                 url: "include/do.php?do=get&db=users&id="+id,
  16.                 dataType: 'JSON',
  17.                 type: 'GET',
  18.                 cache: false,
  19.                 success: function(data){
  20.                     $("#results").append(data);
  21.                     alert(data.params);
  22.                     for (var x = 0; x < data.length; x++) {
  23.                             $("#results").append(data[x].username); 
  24.                     } 
  25.                     $("#add").slideUp("slow");       
  26.                     $("#addform").slideDown("slow", function() {
  27.                         //$(".focus").focus();
  28.                     });
  29.                     $(op).attr("src","images/cancel.png");
  30.                     $(op).attr("title","Cancel Edit");
  31.                 }
  32.             });
  33.            
  34.         } else {
  35.             //cancel the edit
  36.             $("tr").removeClass('selected');
  37.             //Reset images
  38.             $(this).attr("src","images/edit.png");
  39.             $(this).attr("title","Edit");
  40.             $("#addform").slideUp("slow", function() {
  41.                 //$(".focus").focus();
  42.                 $("#add").slideDown("slow");       
  43.             });
  44.         }
  45.     });   
the data returned from the ajax request thingy looks like
  1. [["user_id","1"],["username","simpsonH"],["forename","Homer"],["surname","Simpson"]]
I want the user_id to go into the user_id input, username into the username input, etc

As you can see I have not started on this, I have just got it to display the data in a div.

I would like to use the same code on different pages so the data returned will be slightly different for each page.

eg
  1. [["screen_id","1"],["name","screen-1"]]


Thanks












    • Topic Participants

    • s_cow