0

Can anyone please help me out in this problem. I am in real trouble and I don't know how to go ahead with this.

I have a tr tag which contains multiple div tags in it.
Whenever user clicks on a 'Add' link, the same tr gets cloned with an incremented id and appended to the table.

Now, I want to enter everything under each tr into the mysql database using php. I really don't know how to do this.

The hierarchy is like this:
tr-->td-->div(many)-->input fields(many) like chkbox,textarea,radio,input

The deadline for my project is nearing and I am really stucked upon this.

8
  • What have you tried? What does your code look like? Sounds like something you want to do with AJAX.
    – putvande
    Commented Aug 19, 2013 at 19:48
  • Dave, please can you just put at least an example of the html, without script on the post or on a fiddle so we can try and help you. Commented Aug 19, 2013 at 20:10
  • jsfiddle.net/t2HHh/1
    – Dave Ronak
    Commented Aug 19, 2013 at 20:14
  • Here, I removed so many div sections below it and also when I click on Add, it doesn't add the cloned thing below the same. But, its working in my file.
    – Dave Ronak
    Commented Aug 19, 2013 at 20:15
  • Is there any way you can remove the tables within then top-level <tr>'s? Commented Aug 19, 2013 at 20:54

3 Answers 3

0

have you tried using serialize to get the values of the input fields?

you should be able to do something like $('tr:selected').serialize()

It'd be easier to help if you had some kind of jsfiddle as an example of your problem.

3
  • I will create in a few minutes... Thanks. But, I don't know how to use this serialize function in php and how we will have tr selected.
    – Dave Ronak
    Commented Aug 19, 2013 at 19:50
  • serialize is a jquery function, not a php function. api.jquery.com/serialize
    – Neil S
    Commented Aug 19, 2013 at 19:51
  • how to get valued of input fields? Because on 'onLoad event' there will be only 1 tr, but user might add so many after that. so, how to call that jQuery serialize function using php
    – Dave Ronak
    Commented Aug 19, 2013 at 20:08
0

Since your question doesn't contain any code, I can't be certain but I would start by getting all the <tr>'s and then loop through each of those and get all the input values for that tr_id and add them to an array or json structure. Then you can serialize that and send it via Ajax to PHP to save.

// Initialize an empty array
returnValues = new Array();
$('tr').each(function(index, tr){
    $('input, textarea, checkbox, select', this).each(function(index, input){
        // collect the name and value pairs
        name = input.attr('name');
        value = input.val();
        //now add these into the array.
        returnValues[tr.attr(id)][name][value];
     });
});
// now post the array to php via Ajax 
1
  • Hey Rob, I am not getting what you are trying to sat. I am kind of new to jQeury, but can you please explain me in detail. I am trying to put my code on jFiddle, but not getting what I want. Thanks...
    – Dave Ronak
    Commented Aug 19, 2013 at 20:06
0

The simplest way I see is to give your input fields appropriate names. Then you just handle the form submission like any other.

You can set the input names as arrays to get around this. For example:

<input type="text" name="fieldname[]" value="" />

Then in your POST data in PHP, $_POST['fieldname'] will be an array.

3
  • like there will be more than 50 input fields under each 'tr'. It will be a total mess if I try to change 'id' of everyone when I clone the 'tr'. Moreover, some of the input fields will be dynamic in itself i.e. you can click on '+'sign besides it and add multiple of them, where again I am using what you said above.
    – Dave Ronak
    Commented Aug 19, 2013 at 20:10
  • You can incorporate the row id in the field name as you add them dynamically if that helps you.
    – rc_luke
    Commented Aug 19, 2013 at 20:21
  • I am not adding them dynamically, but I am cloning the entire 'tr', incrementing it's id by 1 and then appending it to the table.
    – Dave Ronak
    Commented Aug 19, 2013 at 20:30

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.