简体   繁体   中英

After insert id in textbox click enter in textbox fill data in form using ajax php sql

I need fill form after insert id in textbox using ajax and php and sql I am need show each value in sprate text box currently return all data in one textbox here code ajax

function getname()
 {
   var id=$("#id").val();   
    $.ajax({
    type:"post",
    dataType:"json",
    data:"id="+id,
    url:"page.php",   
    success:function(json)
    { 
        $("#name").val(json);   

    }
    });

  }

here is code html

<input type="text" name="id" id="id" tabindex="1" onkeypress="getname()">
<input type="text" name="name" id="name" />

here is code php

<?php
  $id=$_POST['id'];
  $db_host = 'MOHAMMAD-PC\SQL2005';
  $db_username = 'sa';
  $db_password = '123321';
  $db_name = 'db_test';
  mssql_connect($db_host, $db_username, $db_password);
  mssql_select_db($db_name); 
  $query=mssql_query("select * from user_info where id='$id'");
  $result=mssql_fetch_assoc($query);

       $json= "{$result['id']}"."{$result['name_ar']}";    
  echo json_encode($json);
exit;

?>

Very Thanks

You echo json-encoded concatenated string instead of array. Change

 $json= "{$result['id']}"."{$result['name_ar']}"; 

to

 $json= array('id'=>$result['id'], 'name'=>$result['name_ar']);

And in ajax function change:

success:function(json)
    { 
        $("#name").val(json);   

    }

to

success:function(json)
    { 

        $("#id").val(json.id);   
        $("#name").val(json.name);   
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM