2016-02-19 50 views
2

Iam使用ajax方法从页面users.php获取数据,并将此页面的结果设置为父页面div中的html内容列表users.php

users.php返回像下面

<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> 

这些值在列表users.php把使用$("#userlists").html();一个DIV中的HTML

<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."); 
      } 
     } 
    }); 
+0

分享php代码也。和ajax代码。 – isnisn

+0

你如何处理提交?通过ajax或你正在阅读$ _POST数组? – mitkosoft

+0

Iam使用$ _POST – Anoop

回答

0

试试这个ajax请求。

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."); 
      } 
     } 
    }); 

你的Ajax响应是HTML,所以你需要添加dataType: "html"

也把所有的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> 

而且在表格标签中也提到Action。可能是这个问题。

+0

ajax请求不是他的问题。'$ _POST ['user_id ']'在ajax请求开始之前是空的 – DestinatioN

0

我想你不是把user_id []当作多维数组。当你阅读帖子时,你错过了关键。它工作时,我试试这个:

$user_ids = $_POST['user_id']; 

foreach($user_ids as $key => $n) { 
    echo "The user_id is ".$n.".PHP_EOL; 
} 
+0

我试过他的例子,对我来说它工作正常... – DestinatioN