My question is this: I have this jquery code where I capture all the questions of a test and just below the alternatives of it; but I can not relate the alternatives to the questions, someone can help me, I tried everything I think.
follows html
<form action="" method="post">
<li><textarea name="quest[]" placeholder="Digite seu texto aqui." required=""></textarea>
<input type="text" id="quest1" name="alternativa[]" value="a)" required="">
<input type="text" id="quest1" name="alternativa[]" value="b)" required=""></li>
<li><textarea name="quest[]" placeholder="Digite seu texto aqui." required=""></textarea>
<input type="text" id="quest2" name="alternativa[]" value="a)" required="">
<input type="text" id="quest2" name="alternativa[]" value="b)" required="">
<input type="text" id="quest2" name="alternativa[]" value="c)" required=""></li></form>
$("body").on('click', 'button[name="enviaProva"]', function (e) {
e.preventDefault();
var textarea = [];
var input = [];
$('textarea[name="quest[]"]').each(function () {
textarea.push($(this).val());
$(this).parent().children('input[name="alternativa[]"]').each(function () {
input.push($(this).val());
});
});
$.ajax({
type: 'POST',
url: '/enviaProva.php',
dataType: "json",
data: {questao: textarea, alternativa: input},
success: function (res) {
}
});
});
The expected result in php would be:
$questao = isset($_POST['questao']) ? $_POST['questao'] : "";
$alternativa = isset($_POST['alternativa']) ? $_POST['alternativa'] : "";
If I give a var_dump in the $ question, it returns me:
array(2) {[0]=> string(12) "primeira questao" [1]=> string(12) "segunda questao"}
no alternative $ returns me:
array(5) { [0]=> string(2) "a)" [1]=> string(2) "b)" [2]=> string(2) "a)" [3]=> string(2) "b)" [4]=> string(2) "c)" }
The alternatives of the first question are "a)" and "b)". And the second question is "a)", "b)" and "C)"
I can not relate the two arrays within php.