[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.
- $(".editbtn").click(function () {
- if ($(this).attr("src") == "images/edit.png") {
- $("#add").slideUp("slow");
- $("#addform").slideUp("slow");
- id = $(this).parents("tr:first").attr("id");
- //Remove all selected classes
- $("tr").removeClass('selected');
- //Reset images
- $(".editbtn").attr("src","images/edit.png");
- $("#"+id).addClass('selected');
- $(this).attr("src","images/load.gif");
- //Load data into form
- var op = $(this);
- $.ajax({
- url: "include/do.php?do=get&db=users&id="+id,
- dataType: 'JSON',
- type: 'GET',
- cache: false,
- success: function(data){
- $("#results").append(data);
- alert(data.params);
- for (var x = 0; x < data.length; x++) {
- $("#results").append(data[x].username);
- }
- $("#add").slideUp("slow");
- $("#addform").slideDown("slow", function() {
- //$(".focus").focus();
- });
- $(op).attr("src","images/cancel.png");
- $(op).attr("title","Cancel Edit");
- }
- });
-
- } else {
- //cancel the edit
- $("tr").removeClass('selected');
- //Reset images
- $(this).attr("src","images/edit.png");
- $(this).attr("title","Edit");
- $("#addform").slideUp("slow", function() {
- //$(".focus").focus();
- $("#add").slideDown("slow");
- });
- }
- });
the data returned from the ajax request thingy looks like
- [["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
- [["screen_id","1"],["name","screen-1"]]
Thanks