如何用JSON编码Web表单?

我有一个Web表单,可从数据库中读取值并将其显示在框中,我想让它最终允许用户对其进行编辑和保存。 当前已经实现了:用户选择记录的ID,并且其中一个字段可以正常工作。我的问题是,当插入ID时,如何在JSON中对该表单进行编码以显示整个记录? 谢谢。 我当前的前端代码:

 var id=jQuery("#myid").val();
 jQuery.getJSON('find.php',{num:id},function(obj){

if (obj.length>0){ 
 jQuery("input.fname").val(data); 
 jQuery("input.sname").val(data); 
 jQuery("input.age").val(data); 
  } 

});

后端代码:

 $id = $_GET['id'];

 $query = "SELECT * FROM Customers WHERE ID = $id";

 $result = array('FirstName' => 1, 'Surname' => 2, 'Age' => 3);

 echo json_encode($result);

哪里出问题了?

To encode just the data in php you can use:

json_encode($data);

and to decode them:

json_decode($json_data);

more reference here: http://www.php.net/manual/en/ref.json.php

In your frontend, when a user clicks on id(button/anchor) to edit the corresponding record, you can request for a json from backend.

var id=$("#recordID").val();  //assuming its an input
$.getJSON('file.php',{num:id},function(obj){

//assign values to your input fields using this obj
$("input.fname").attr("value",obj.FirstName); 
$("input.sname").attr("value",obj.SurName); 
$("input.age").attr("value",obj.Age); 
});

In the backend,

$id=$_GET['num'];
//select * from yourtable where id=$id

//encode the result array(associative) using json_encode
//echo the json object