So,I have an array of input values:
<input type="text" placeholder="write your Question" id="question[]" value="" />
and I'm sending this inputs through this code:
$.post("function.php",{Question:$("#question").serialize()},function(data){
$("#construct").append(data);
alert('done');
});
But when I try to use these values in my PHP,I have some errors.
PHP(function.php):
$Question=htmlentities($_POST['Question'],ENT_QUOTES,"UTF-8");
//line 13
$Quiz->InsertQuestion($Q_id,$Question[0]);
//line 14
$Quiz->InsertQuestion($Q_id,$Question[1]);
the error says:
Notice: Uninitialized string offset: 0 in E:\program file\program\xampp\htdocs\QMS\admin\function.php on line 13
Notice: Uninitialized string offset: 1 in E:\progrram file\program\xampp\htdocs\QMS\admin\function.php on line 14
thanks in advance.
As I said, id="question[]"
and id="question"
are different ids. If you want to send items as an array you should use name
attribute with []
:
<input type="text" placeholder="write your Question" name="question[]" value="" />
<input type="text" placeholder="write your Question" name="question[]" value="" />
jquery:
// select all fields which names start with "question"
{Question:$("[name^='question']").serialize()}
or even class
:
<input type="text" placeholder="write your Question" value="" class="question" />
<input type="text" placeholder="write your Question" value="" class="question" />
jquery:
{Question:$(".question").serialize()}
Change the id of input from "question[]" to "question".