0

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.

3
  • var_dump($_POST['Question']); And btw question[] and question are different ids. Commented Sep 6, 2015 at 8:59
  • @u_mulder I used var_dump and I got:string(0) "" .btw I thought when I want to send an array so I should use question[]! Commented Sep 6, 2015 at 9:04
  • Can you post all of your html? Commented Sep 6, 2015 at 9:05

2 Answers 2

1

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()}
Sign up to request clarification or add additional context in comments.

1 Comment

it seems when I use .serialize() I can't get the complete value
0

Change the id of input from "question[]" to "question".

3 Comments

Why did he want to change that if he wants to pass an array of questions?
Because it is an id of a DOM content.
@Ahmadbaba46 I did it but still failed:-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.