0

I have form like this:

<input type='text' name='people' id='namepeople'>
<input type='hidden' name='idpeople' id='idpeople'>
<input type='text' name='address' id='address' disabled>
<input type='text' name='age' id='age' disabled>
<input type='text' name='status' id='status' disabled>

and a jQuery script:

$("#namepeople").keyup(function()
{ 
  var namepeople= $(this).val();
  var name= namepeople;

     $.ajax({
        type: "POST",
        url: "check.php",
        data: "namepeople=" + name,
        dataType: "json",
        success: function(data)
        {    
            $("#idpeople").val(data[0]);
            $("#address").val(data[1]);
            $("#age").val(data[2]);
            $("#status").val(data[3]);
        }
     });//ajax
});//keyup

And php code :

<?
mysql_connect("localhost","root","");
mysql_select_db("dbajax");


$name = $_POST["namepeople"];
$sql = mysql_query("select * from anggota where upper(username) like '%$name'");
$row = mysql_fetch_array($sql);
$kt=mysql_num_rows($sql);

if($kt > 0)
{
  $idnya=$row["id_anggota"];
  $address=$row["address"];
  $age=$row["age"];
  $status=$row["status"];

}
else
{
 $idnya="";
 $address="";
 $age="";
 $status="";
}
 echo "$idnya|$address|$age|$status";
 ?>

This is working out just for value of idpeople, but the other value are not coming out. How to get value on multiple textbox with jQuery?

1
  • This echo "$idnya|$address|$age|$status"; does not look like json to me Commented Jan 20, 2012 at 3:37

2 Answers 2

1

You should modify your ajax call to send the entire form, because right now it only sends idpeople. You should use $('#myForm').serialize() as shown below, where myForm is the id of your form element. Then, within php you can access all your data with $_POST like $_POST['people'] for example. BUT, if you have your inputs disabled like in your example, they will not be sent to your PHP.

$.ajax({
type: "POST",
url: "check.php",
data: $('#myForm').serialize(),
dataType: "json",
success: function(data)
    {    
      $("#idpeople").val(data[0]);
      $("#address").val(data[1]);
      $("#age").val(data[2]);
      $("#status").val(data[3]);
}

You also need to change your php to return JSON. change the last line of your php to this:

echo json_encode(array($idnya,$address,$age,$status));

Sign up to request clarification or add additional context in comments.

Comments

1

Your issue is on the return echo I believe. do the following:.

$res = array($idnya,$address,$age,$status);
$json = json_encode($res);
echo $json;
exit();

And you should be good

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.