jquery - How to save many text inputs as a PHP string? -


basically, need construct large string out of bunch on html text inputs.

firstly, these text inputs being dynamically created button, therefore, there can amount of inputs user wants.

here format of each individual dynamically created text input:

[question]   [incorrect-answer1]              [incorrect-answer2]              [incorrect-answer3]              [correct-answer]              remove 

each item surrounded [] text input, along 'remove' being button removes current question.

this whole jquery function creating each dynamic question:

function dynamicform () {  //set counter var = $('.dynamic-input#form-step2').length + 1; //alert(i); //add input $('a#add').click(function () {     $('<table><tr><td><p><span class="left"><input type="text" class="dynamic-input" name="items[]" id="' + + '" placeholder="question" /></span>' +         '<span class="right"><input type="text" class="dynamic-input" name="items[]" id="' + + '" placeholder="distraction 1" /><br /><input type="text" class="dynamic-input" name="items[]" id="' + + '" placeholder="distraction 2" /><br /><input type="text" class="dynamic-input" name="items[]" id="' + + '" placeholder="distraction 3" /><br /><input type="text" class="dynamic-input" name="items[]" id="' + + '" placeholder="correct answer" /><br /><a href="#">remove</a></span></p></td></tr></table>').fadein("slow").appendto('#extender');     i++;      $("a:contains('remove')").click(function () {             $(this).parent().parent().remove();     });      return false; });   //fadeout selected item , remove $("#form-step2.dynamic-input").on('click', 'a', function () {     $(this).parent().fadeout(300, function () {         $(this).empty();         return false;     }); }); } 

and simple little button creates each question:

<a id="add" href="#">add question</a> 

what need achieve:

once button pressed, need somehow gather question elements, , save them string. format each question must saved into:

question1,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer question1,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer question2,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer 

as can see above, there total of 3 questions. each question must separated line-break, being '\n'. order must question, 3 incorrect answers, following correct answer; separated commas.

of course, not asking me. need guidance , support since still newbie php , jquery (learning php 8 weeks, jquery 2 weeks). of code constructed existing code here @ stack overflow, , other online sources.

all appreciated

as perhaps more flexible alternative array_chunk (since may have more "incorrect answers" in future , require keep updating array_chunk size param), can go following:

in javascript snippet, populating dom elements, can populate name attribute multidimensional array (i renamed items questions conceptual purposes):

$('<table><tr><td><p><span class="left"><input type="text" class="dynamic-input" name="questions[' +i +'][]" id="' + + '" placeholder="question" /></span>' +         '<span class="right"><input type="text" class="dynamic-input" name="questions[' +i +'][]" id="' + + '" placeholder="distraction 1" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][]" id="' + + '" placeholder="distraction 2" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][]" id="' + + '" placeholder="distraction 3" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][]" id="' + + '" placeholder="correct answer" /><br /><a href="#">remove</a></span></p></td></tr></table>').fadein("slow").appendto('#extender'); 

so array of question arrays.

in php side (similar array_chunk post), can use implode such:

if(!empty($_post['questions'])) {     foreach($_post['questions'] $question) {         echo implode(',',$question) ."\n";     } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -