このデモでは、フォームから入力を取得し、それらを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;
?>