2

Iam using ajax method for fetching data from a page users.php and the results from this page are set as html inside a div in parent page list-users.php

The users.php returns the html like below

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

These values are put inside a div using $("#userlists").html(); in list-users.php

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

When I submit the form, the user_id[] always returns empty. I don't know what the error is. Is it possible to get the values of user_id[] while submitting form?

<?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.");
            }
        }
    });
Anoop
  • 252
  • 1
  • 4
  • 12

2 Answers2

0

Try with this ajax request.

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

Your ajax response is html, so you need to add dataType: "html"

Also put all html in one line like this.

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

And mention Action in form tag too. May be this would be the problem.

Believe It or Not
  • 631
  • 2
  • 8
  • 21
0

I think you are not treating user_id[] as multidimensional arrays. When you read the post you are missing out on the key. It works when i try this:

$user_ids = $_POST['user_id'];

foreach( $user_ids as $key => $n ) {
  echo "The user_id is ".$n.".PHP_EOL;
}
isnisn
  • 254
  • 1
  • 7