ajax附加输入字段的$ _POST始终返回空值

时间:2016-02-19 09:40:31

标签: php jquery html ajax

我使用ajax方法从页面users.php获取数据,并且此页面的结果在父页面list-users.php中的div内设置为html

users.php 会返回如下所示的html

<table width="100%" border="0">
  <tr>
    <td>John<input name="user_id[]" type="hidden" value="1" /></td>
  </tr>
  <tr>
    <td>Sam<input name="user_id[]" type="hidden" value="2" /></td>
  </tr>
</table>

使用 list-users.php

中的$("#userlists").html();将这些值放入div中
<form method="post">
<div id="userlists">
<table width="100%" border="0">
  <tr>
    <td>John<input name="user_id[]" type="hidden" value="1" /></td>
  </tr>
  <tr>
    <td>Sam<input name="user_id[]" type="hidden" value="2" /></td>
  </tr>
</table>
</div>
<input type="submit" name="submit_users" value="Save" />
</form>

当我提交表单时,user_id[]始终返回空。我不知道错误是什么。提交表单时是否可以获得user_id[]的值?

<?php
if(isset($_POST['submit_users']))
{
 print_r($_POST['user_id']);
//this returns empty data always
}
?>

var value='{"slno":"'+number+'","empcode":"'+code+'"}';
    jQuery.ajax({       
        type: 'POST',
        url: 'inc/users.php',
        data: 'values='+value,
        cache: false,
        success: function(html){
            if(html)
            {               
                $("#userlists").html(html);
            }
            else
            {
                $("#userlists").html("Error Occured.");
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

尝试使用此ajax请求。

modulePathIgnorePatterns

您的ajax回复是html,因此您需要添加var value='{"slno":"'+number+'","empcode":"'+code+'"}'; jQuery.ajax({ type: 'POST', url: 'inc/users.php', cache: false, data: { "values" : value }, dataType: "html" success: function(html){ if(html) { $("#userlists").html(html); } else { $("#userlists").html("Error Occured."); } } });

同样将所有html放在一行中。

dataType: "html"

并在表单标签中提及Action。可能这就是问题所在。

答案 1 :(得分:0)

我认为您不会将user_id []视为多维数组。当你阅读帖子时,你错过了钥匙。当我尝试这个时它起作用:

$user_ids = $_POST['user_id'];

foreach( $user_ids as $key => $n ) {
  echo "The user_id is ".$n.".PHP_EOL;
}
相关问题