I do generate some <input>
s with random names on the page (nearly ten at once).
Stuck on a question about submitting them.
I use this code to submit:
$('.table_data').submit(function(event)
{
event.preventDefault();
// some validation check and then
$.post(url, {user_mail: mail}, function()
{
alert('done);
});
});
The problem is, it submits user_mail
only.
How do I submit data of all <input>
s?
Have tried to use $.post(url, function(){})
, it sends nothing.
Its possible to get all the inputs into array and then throw to the $.post
function, but I'm not sure how to do it well.
I do generate some <input>
s with random names on the page (nearly ten at once).
Stuck on a question about submitting them.
I use this code to submit:
$('.table_data').submit(function(event)
{
event.preventDefault();
// some validation check and then
$.post(url, {user_mail: mail}, function()
{
alert('done);
});
});
The problem is, it submits user_mail
only.
How do I submit data of all <input>
s?
Have tried to use $.post(url, function(){})
, it sends nothing.
Its possible to get all the inputs into array and then throw to the $.post
function, but I'm not sure how to do it well.
3 Answers
Reset to default 6If your form's classname is table_data
then use this
$.post(url,$('.table_data').serialize(), function(){})
And you will get form input's names
as key of your global POST
array.
Full code
$('.table_data').submit(function(event)
{
event.preventDefault();
// some validation check and then
$.post(url, $(this).serialize(), function()
{
alert('done');
});
});
Read more on $.serialize
$.post(
url, // url to submit
$(this).serialize(), // make a standard-looking query string using all inputs value
function(response) {
// success function
// in parameter response is to capture data send from capture
},
'json' // dataTyep in which format you're accepting
// data from server may be html, xml ans so on
);
Related refs:
$.serialize()
$.post()
You are preventing the default behaviour on form submit using event.preventDefault()
and then only sending the usermail value to server using ajax.
From what I understand, you need to serialize the form so that you can obtain all the names and their values and then send it to server. For that you need serialize function in jQuery