dqby43944
2015-09-06 08:53
浏览 62

无法通过Jquery.post发送数组

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.

图片转代码服务由CSDN问答提供 功能建议

所以,我有一个输入值数组:

   &lt; input type =“text”placeholder =“write your Question”id =“question []”value =“”/&gt; 
   
 
 

我是 通过以下代码发送此输入:

  $ .post(“function.php”,{Question:$(“#question”)。serialize()},function(data  ){
 $(“#construct”)。append(data); 
 alert('done'); 
}); 
   
 
 

但是当 我尝试在我的PHP中使用这些值,我有一些错误。

PHP(function.php):

  $ Question =  htmlentities($ _ POST ['Question'],ENT_QUOTES,“UTF-8”); 
 //第13行
 $ Quiz-&gt; InsertQuestion($ Q_id,$ Question [0]); 
 //第14行 
 $ Quiz-&gt; InsertQuestion($ Q_id,$ Question [1]); 
   
 
 

错误说:

注意:未初始化的字符串偏移量:在第13行的E:\ program file \ program \ xampp \ htdocs \ QMS \ admin \ function.php中为0

注意:未初始化 字符串偏移量:E:\ progrram file \ program \ x中的1 第14行的ampp \ htdocs \ QMS \ admin \ function.php

提前感谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duancai7568 2015-09-06 09:08
    已采纳

    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()}
    
    点赞 评论
  • doulin8374 2015-09-06 09:01

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

    点赞 评论

相关推荐 更多相似问题