对于这个演示,我从表单获取输入,将它们放入 json 对象并通过 ajax 发送到服务器。如何使用服务器上的数据重新填充表单?我可以让它在控制台中打印,但似乎无法弄清楚如何以表格形式获取它。就像在表单字段中的某处放置类似 jsonObject.firstName 之类的东西一样简单吗?
形式:
div class="form">
<form method="GET" action="demo.html">
<section class="formSection">
<div class="dataChunk">
<label for="firstName">First Name:</label>
<input type="text" id="jsonObject.firstName" maxlength="50" />
</div>
<div class="dataChunk">
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" maxlength="50" />
</div>
<div class="dataChunk">
<label for="phoneNumber">Phone:</label>
<input type="text" id="phoneNumber" maxlength="10" />
</div>
<div class="dataChunk">
<label for="address">Address:</label>
<input type="text" id="address" maxlength="50" />
</div>
</section>
</form>
</div>
json对象
var jsonObject = {
"firstName" : firstName,
"lastName" : lastName,
"phone" : phone,
"address" : address
};
php
$name = $_REQUEST['firstName']." ".$_REQUEST['lastName'];
$phone = $_REQUEST['phone'];
$address = $_REQUEST['address'];
$person = Array();
$person['name'] = $name;
$person['phone'] = $phone;
$person['address'] = $address;
$returnObj = json_encode($person);
echo $returnObj;
?>