Add id from element inside already serialized form jquery
I have a dynamic list which can be ordered, the id
of these elements changes accordingly.
My problem: I serialize the entire form and send it through ajax to a PHP script, but the id
is not sent with that.
My array now looks like this for example:
Array
(
[title] => lijsttitle
[Category 1] => Array
(
[0] => Question 1
)
[Category 2] => Array
(
[0] => Question 1
)
[Category 3] => Array
(
[0] => Question 1
)
[Category 4] => Array
(
[0] => Question 1
)
)
My HTML form which when posted generates above array looks like this:
<form id="lijstform">
<div class="row">
<div class="col-md-8">
<label class="lijstnaamtitle">Lijst naam</label>
<input class="form-control name_list catinput lijsttitle" type="text" name="lijsttitle">
</div>
</div>
<div id="dynamic_field" class="ui-sortable">
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
</div>
<div class="row sortwrap ui-sortable-handle" id="2">
<div class="col-md-8">
<input type="text" name="category" placeholder="2. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
<div class="row sortwrap" id="3">
<div class="col-md-8">
<input type="text" name="category" placeholder="3. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
<div class="row sortwrap" id="4">
<div class="col-md-8">
<input type="text" name="category" placeholder="4. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
</div>
</form>
As you can see every sortable row has an id: <div class="row sortwrap" id="4">
I would like that id inside my array for every category so I can update my database and later display the saved list in its correct order.
I've tried the following:
$( ".lijstbutton" ).on( "click", function( event ) {
event.preventDefault();
url = 'includes/createlist.php';
$lijst = $( '#lijstform' ).serializeArray();
$idorder = $('.sortwrap').attr('id');
var posting = $.post(url, {
lijst: $lijst,
idorder: $idorder
});
posting.done(function( data ) {
$( ".lijstresult" ).empty().slideDown('fast').append( data );
});
});
But when posted this is what I see in my network tab:
lijst[0][name]: lijsttitle
lijst[0][value]: List name
lijst[1][name]: category
lijst[1][value]: Category 1
lijst[2][name]: question
lijst[2][value]: Question 1
lijst[3][name]: category
lijst[3][value]: Category 2
lijst[4][name]: question
lijst[4][value]: Question 1
lijst[5][name]: category
lijst[5][value]: Category 3
lijst[6][name]: question
lijst[6][value]: Question 1
lijst[7][name]: category
lijst[7][value]: Category 4
lijst[8][name]: question
lijst[8][value]: Question 1
idorder: 1
Only the first one is posted.
This is how I generate the array in my PHP script:
$arr = $_POST['lijst'];
$store = ;
// pull off first arr element
$title = array_shift($arr);
// save title to store
$store['title'] = $title['name'];
$currCat = '';
foreach($arr as $a) {
$val = $a['value'];
// handle category
if($a['name'] == 'category') {
// save cat name
$currCat = $val;
// init questions array
$store[$currCat] = ;
}
else {
// add question to question array
$store[$currCat] = $val;
}
}
Not sure what the best method is to add the order id to my array and how to do it.
Maybe add the id
to every key after a barely used character? Something like [Category 1|1] and then explode on |
javascript php jquery
add a comment |
I have a dynamic list which can be ordered, the id
of these elements changes accordingly.
My problem: I serialize the entire form and send it through ajax to a PHP script, but the id
is not sent with that.
My array now looks like this for example:
Array
(
[title] => lijsttitle
[Category 1] => Array
(
[0] => Question 1
)
[Category 2] => Array
(
[0] => Question 1
)
[Category 3] => Array
(
[0] => Question 1
)
[Category 4] => Array
(
[0] => Question 1
)
)
My HTML form which when posted generates above array looks like this:
<form id="lijstform">
<div class="row">
<div class="col-md-8">
<label class="lijstnaamtitle">Lijst naam</label>
<input class="form-control name_list catinput lijsttitle" type="text" name="lijsttitle">
</div>
</div>
<div id="dynamic_field" class="ui-sortable">
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
</div>
<div class="row sortwrap ui-sortable-handle" id="2">
<div class="col-md-8">
<input type="text" name="category" placeholder="2. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
<div class="row sortwrap" id="3">
<div class="col-md-8">
<input type="text" name="category" placeholder="3. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
<div class="row sortwrap" id="4">
<div class="col-md-8">
<input type="text" name="category" placeholder="4. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
</div>
</form>
As you can see every sortable row has an id: <div class="row sortwrap" id="4">
I would like that id inside my array for every category so I can update my database and later display the saved list in its correct order.
I've tried the following:
$( ".lijstbutton" ).on( "click", function( event ) {
event.preventDefault();
url = 'includes/createlist.php';
$lijst = $( '#lijstform' ).serializeArray();
$idorder = $('.sortwrap').attr('id');
var posting = $.post(url, {
lijst: $lijst,
idorder: $idorder
});
posting.done(function( data ) {
$( ".lijstresult" ).empty().slideDown('fast').append( data );
});
});
But when posted this is what I see in my network tab:
lijst[0][name]: lijsttitle
lijst[0][value]: List name
lijst[1][name]: category
lijst[1][value]: Category 1
lijst[2][name]: question
lijst[2][value]: Question 1
lijst[3][name]: category
lijst[3][value]: Category 2
lijst[4][name]: question
lijst[4][value]: Question 1
lijst[5][name]: category
lijst[5][value]: Category 3
lijst[6][name]: question
lijst[6][value]: Question 1
lijst[7][name]: category
lijst[7][value]: Category 4
lijst[8][name]: question
lijst[8][value]: Question 1
idorder: 1
Only the first one is posted.
This is how I generate the array in my PHP script:
$arr = $_POST['lijst'];
$store = ;
// pull off first arr element
$title = array_shift($arr);
// save title to store
$store['title'] = $title['name'];
$currCat = '';
foreach($arr as $a) {
$val = $a['value'];
// handle category
if($a['name'] == 'category') {
// save cat name
$currCat = $val;
// init questions array
$store[$currCat] = ;
}
else {
// add question to question array
$store[$currCat] = $val;
}
}
Not sure what the best method is to add the order id to my array and how to do it.
Maybe add the id
to every key after a barely used character? Something like [Category 1|1] and then explode on |
javascript php jquery
add<input type="hidden" name="id" value="id-here" />
in every row that needs an id
– Nikos M.
Nov 15 '18 at 9:42
lookupeach
in the jQuery Manual
– RiggsFolly
Nov 15 '18 at 9:49
add a comment |
I have a dynamic list which can be ordered, the id
of these elements changes accordingly.
My problem: I serialize the entire form and send it through ajax to a PHP script, but the id
is not sent with that.
My array now looks like this for example:
Array
(
[title] => lijsttitle
[Category 1] => Array
(
[0] => Question 1
)
[Category 2] => Array
(
[0] => Question 1
)
[Category 3] => Array
(
[0] => Question 1
)
[Category 4] => Array
(
[0] => Question 1
)
)
My HTML form which when posted generates above array looks like this:
<form id="lijstform">
<div class="row">
<div class="col-md-8">
<label class="lijstnaamtitle">Lijst naam</label>
<input class="form-control name_list catinput lijsttitle" type="text" name="lijsttitle">
</div>
</div>
<div id="dynamic_field" class="ui-sortable">
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
</div>
<div class="row sortwrap ui-sortable-handle" id="2">
<div class="col-md-8">
<input type="text" name="category" placeholder="2. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
<div class="row sortwrap" id="3">
<div class="col-md-8">
<input type="text" name="category" placeholder="3. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
<div class="row sortwrap" id="4">
<div class="col-md-8">
<input type="text" name="category" placeholder="4. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
</div>
</form>
As you can see every sortable row has an id: <div class="row sortwrap" id="4">
I would like that id inside my array for every category so I can update my database and later display the saved list in its correct order.
I've tried the following:
$( ".lijstbutton" ).on( "click", function( event ) {
event.preventDefault();
url = 'includes/createlist.php';
$lijst = $( '#lijstform' ).serializeArray();
$idorder = $('.sortwrap').attr('id');
var posting = $.post(url, {
lijst: $lijst,
idorder: $idorder
});
posting.done(function( data ) {
$( ".lijstresult" ).empty().slideDown('fast').append( data );
});
});
But when posted this is what I see in my network tab:
lijst[0][name]: lijsttitle
lijst[0][value]: List name
lijst[1][name]: category
lijst[1][value]: Category 1
lijst[2][name]: question
lijst[2][value]: Question 1
lijst[3][name]: category
lijst[3][value]: Category 2
lijst[4][name]: question
lijst[4][value]: Question 1
lijst[5][name]: category
lijst[5][value]: Category 3
lijst[6][name]: question
lijst[6][value]: Question 1
lijst[7][name]: category
lijst[7][value]: Category 4
lijst[8][name]: question
lijst[8][value]: Question 1
idorder: 1
Only the first one is posted.
This is how I generate the array in my PHP script:
$arr = $_POST['lijst'];
$store = ;
// pull off first arr element
$title = array_shift($arr);
// save title to store
$store['title'] = $title['name'];
$currCat = '';
foreach($arr as $a) {
$val = $a['value'];
// handle category
if($a['name'] == 'category') {
// save cat name
$currCat = $val;
// init questions array
$store[$currCat] = ;
}
else {
// add question to question array
$store[$currCat] = $val;
}
}
Not sure what the best method is to add the order id to my array and how to do it.
Maybe add the id
to every key after a barely used character? Something like [Category 1|1] and then explode on |
javascript php jquery
I have a dynamic list which can be ordered, the id
of these elements changes accordingly.
My problem: I serialize the entire form and send it through ajax to a PHP script, but the id
is not sent with that.
My array now looks like this for example:
Array
(
[title] => lijsttitle
[Category 1] => Array
(
[0] => Question 1
)
[Category 2] => Array
(
[0] => Question 1
)
[Category 3] => Array
(
[0] => Question 1
)
[Category 4] => Array
(
[0] => Question 1
)
)
My HTML form which when posted generates above array looks like this:
<form id="lijstform">
<div class="row">
<div class="col-md-8">
<label class="lijstnaamtitle">Lijst naam</label>
<input class="form-control name_list catinput lijsttitle" type="text" name="lijsttitle">
</div>
</div>
<div id="dynamic_field" class="ui-sortable">
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
</div>
<div class="row sortwrap ui-sortable-handle" id="2">
<div class="col-md-8">
<input type="text" name="category" placeholder="2. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
<div class="row sortwrap" id="3">
<div class="col-md-8">
<input type="text" name="category" placeholder="3. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
<div class="row sortwrap" id="4">
<div class="col-md-8">
<input type="text" name="category" placeholder="4. Voeg een categorie toe" class="form-control name_list catinput"> <i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput"> </div>
<div class="col-md-4"> </div>
</div>
</div>
</div>
<div class="col-md-4">
<button id="addcategory" class="btn btn-danger btn_remove removebutton">X</button>
</div>
</div>
</div>
</form>
As you can see every sortable row has an id: <div class="row sortwrap" id="4">
I would like that id inside my array for every category so I can update my database and later display the saved list in its correct order.
I've tried the following:
$( ".lijstbutton" ).on( "click", function( event ) {
event.preventDefault();
url = 'includes/createlist.php';
$lijst = $( '#lijstform' ).serializeArray();
$idorder = $('.sortwrap').attr('id');
var posting = $.post(url, {
lijst: $lijst,
idorder: $idorder
});
posting.done(function( data ) {
$( ".lijstresult" ).empty().slideDown('fast').append( data );
});
});
But when posted this is what I see in my network tab:
lijst[0][name]: lijsttitle
lijst[0][value]: List name
lijst[1][name]: category
lijst[1][value]: Category 1
lijst[2][name]: question
lijst[2][value]: Question 1
lijst[3][name]: category
lijst[3][value]: Category 2
lijst[4][name]: question
lijst[4][value]: Question 1
lijst[5][name]: category
lijst[5][value]: Category 3
lijst[6][name]: question
lijst[6][value]: Question 1
lijst[7][name]: category
lijst[7][value]: Category 4
lijst[8][name]: question
lijst[8][value]: Question 1
idorder: 1
Only the first one is posted.
This is how I generate the array in my PHP script:
$arr = $_POST['lijst'];
$store = ;
// pull off first arr element
$title = array_shift($arr);
// save title to store
$store['title'] = $title['name'];
$currCat = '';
foreach($arr as $a) {
$val = $a['value'];
// handle category
if($a['name'] == 'category') {
// save cat name
$currCat = $val;
// init questions array
$store[$currCat] = ;
}
else {
// add question to question array
$store[$currCat] = $val;
}
}
Not sure what the best method is to add the order id to my array and how to do it.
Maybe add the id
to every key after a barely used character? Something like [Category 1|1] and then explode on |
javascript php jquery
javascript php jquery
asked Nov 15 '18 at 9:38
twantwan
97921436
97921436
add<input type="hidden" name="id" value="id-here" />
in every row that needs an id
– Nikos M.
Nov 15 '18 at 9:42
lookupeach
in the jQuery Manual
– RiggsFolly
Nov 15 '18 at 9:49
add a comment |
add<input type="hidden" name="id" value="id-here" />
in every row that needs an id
– Nikos M.
Nov 15 '18 at 9:42
lookupeach
in the jQuery Manual
– RiggsFolly
Nov 15 '18 at 9:49
add
<input type="hidden" name="id" value="id-here" />
in every row that needs an id– Nikos M.
Nov 15 '18 at 9:42
add
<input type="hidden" name="id" value="id-here" />
in every row that needs an id– Nikos M.
Nov 15 '18 at 9:42
lookup
each
in the jQuery Manual– RiggsFolly
Nov 15 '18 at 9:49
lookup
each
in the jQuery Manual– RiggsFolly
Nov 15 '18 at 9:49
add a comment |
1 Answer
1
active
oldest
votes
Easy approach will be to create hidden
input under each row sortwrap
which will get posted to when form is submitted.
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
<input type="hidden" name="id" value="1" /> <!-- POST ID in Hidden input -->
</div>
Similarly, do it under all sortwrap container.
Another approach using your current flow is to use jQuery map function to retrieve all IDs.
Change
$idorder = $('.sortwrap').attr('id');
To
$idorder = $('.sortwrap').map(function() {
return $(this).attr("id");
}).get();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316397%2fadd-id-from-element-inside-already-serialized-form-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Easy approach will be to create hidden
input under each row sortwrap
which will get posted to when form is submitted.
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
<input type="hidden" name="id" value="1" /> <!-- POST ID in Hidden input -->
</div>
Similarly, do it under all sortwrap container.
Another approach using your current flow is to use jQuery map function to retrieve all IDs.
Change
$idorder = $('.sortwrap').attr('id');
To
$idorder = $('.sortwrap').map(function() {
return $(this).attr("id");
}).get();
add a comment |
Easy approach will be to create hidden
input under each row sortwrap
which will get posted to when form is submitted.
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
<input type="hidden" name="id" value="1" /> <!-- POST ID in Hidden input -->
</div>
Similarly, do it under all sortwrap container.
Another approach using your current flow is to use jQuery map function to retrieve all IDs.
Change
$idorder = $('.sortwrap').attr('id');
To
$idorder = $('.sortwrap').map(function() {
return $(this).attr("id");
}).get();
add a comment |
Easy approach will be to create hidden
input under each row sortwrap
which will get posted to when form is submitted.
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
<input type="hidden" name="id" value="1" /> <!-- POST ID in Hidden input -->
</div>
Similarly, do it under all sortwrap container.
Another approach using your current flow is to use jQuery map function to retrieve all IDs.
Change
$idorder = $('.sortwrap').attr('id');
To
$idorder = $('.sortwrap').map(function() {
return $(this).attr("id");
}).get();
Easy approach will be to create hidden
input under each row sortwrap
which will get posted to when form is submitted.
<div class="row sortwrap ui-sortable-handle" id="1">
<div class="col-md-8">
<input type="text" name="category" placeholder="1. Voeg een categorie toe" class="form-control name_list catinput">
<i class="mdi mdi-sort dragndrop"></i>
<div class="questionlist questionwrap">
<div class="row">
<div class="col-md-8">
<button class="btn btn-success questionbutton">Extra vraag</button>
<input type="text" name="question" placeholder="1. Voeg een vraag toe" class="form-control name_list questioninput">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="col-md-4">
</div>
<input type="hidden" name="id" value="1" /> <!-- POST ID in Hidden input -->
</div>
Similarly, do it under all sortwrap container.
Another approach using your current flow is to use jQuery map function to retrieve all IDs.
Change
$idorder = $('.sortwrap').attr('id');
To
$idorder = $('.sortwrap').map(function() {
return $(this).attr("id");
}).get();
edited Nov 15 '18 at 10:03
answered Nov 15 '18 at 9:44
SamirSamir
5,3242628
5,3242628
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316397%2fadd-id-from-element-inside-already-serialized-form-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
add
<input type="hidden" name="id" value="id-here" />
in every row that needs an id– Nikos M.
Nov 15 '18 at 9:42
lookup
each
in the jQuery Manual– RiggsFolly
Nov 15 '18 at 9:49