0

I know there have been lots of questions about this and I have it working upto a point..

My form is designed as :

 <tr data-id='123456789'>
 <td><input name="Site" type="text" id="Site" value='123'/></td>
 <td><input name="Location" type="text" id="Location" value='NW'/></td>
 </tr>
 <tr data-id='987654321'>
 <td><input name="Site" type="text" id="Site" value='444'/></td>
 <td><input name="Location" type="text" id="Location" value='NE'/></td>
 </tr>

I'm then serialising using :

var datastring = $("#form2").serialize();

This works but doesn't include the data-id from the TR is there anyway I can include that ?

What I want to send to my php page is :

123456789:123:NW
987654321:444:NE

I'm happy to go with a better way of doing this ! Thanks

asked Aug 22, 2016 at 11:27
1
  • you can pass data-id in hidden field Commented Aug 22, 2016 at 12:20

3 Answers 3

2

Here I have write some code this will be help full to you. you can do it instead of serialize of form.

var post_data={data:[]};
$('tr[data-id]').each(function(){
 var tmp=[];
 tmp.push($(this).data('id'));
 $(this).find('input').each(function(){
 tmp.push($(this).val());
 });
 post_data.data.push(tmp.join(":"));
});
alert(JSON.stringify(post_data));
// now you can post data as a form using ajax.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr data-id='123456789'>
 <td><input name="Site" type="text" id="Site" value='123'/></td>
 <td><input name="Location" type="text" id="Location" value='NW'/></td>
 </tr>
 <tr data-id='987654321'>
 <td><input name="Site" type="text" id="Site" value='444'/></td>
 <td><input name="Location" type="text" id="Location" value='NE'/></td>
 </tr>
 </table>

answered Aug 22, 2016 at 11:37
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that takes the data I need.
0

Use hidden input and set all the values in arrays. Don't use ID's or names multiple times.

<tr data-id='123456789'>
 <td>
 <input name="id[]" type="hidden" value='123456789'/>
 <input name="Site[]" type="text" value='123'/>
 </td>
 <td>
 <input name="Location[]" type="text" value='NW'/>
 </td>
</tr>
<tr data-id='987654321'>
 <td>
 <input name="id[]" type="hidden" value='987654321'/>
 <input name="Site[]" type="text" value='444'/>
 </td>
 <td>
 <input name="Location[]" type="text"value='NE'/>
 </td>
</tr>
answered Aug 22, 2016 at 11:33

Comments

0
<tr>
 <td><input name="data_id[]" type="text" id="data_id" value='123456789'/></td>
 <td><input name="Site[]" type="text" id="Site" value='123'/></td>
 <td><input name="Location[]" type="text" id="Location" value='NW'/></td>
</tr>
<tr>
 <td><input name="data_id[]" type="text" id="data_id" value='987654321'/></td>
 <td><input name="Site[]" type="text" id="Site" value='444'/></td>
 <td><input name="Location[]" type="text" id="Location" value='NE'/></td>
</tr>

Fetch all three array in php page and process them in a loop, You can process Site and location on the basis of data_id

answered Aug 22, 2016 at 11:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.