我能够通过以下代码获取json文件:
<?php
include "lib/lib.php";
$url = "http://10.0.0.1/lib/api/desk/";
$params = array ("action" => "list","company_key" => "1");
$result=requestURL($url,$params);
$json_a=json_decode(strip_tags($result));
?>
结果是:
{
"data": [
{
"id": "18",
"name": "SM Quezon",
"branch_address": "Quezon City, Philippines",
"officer_in_charge": "Juan Dela Cruzz",
"contact_number": "09321234567, 02-3449067"
}
]
}
我有一个表单,它应该用于将数据数组添加到json文件中。应该发生的是,在提交表单后,插入的数据现在将包含在json文件中。
<div class="modal-body">
<form id="form" onsubmit="alert('save?')" method="post">
<div class="modal-body">
<label class="control-label">Name</label>
<input type="text" class="form-control" id="Name" />
<label class="control-label">Branch Address</label>
<input type="text" class="form-control" id="BranchAddress" />
<label class="control-label">Officer-in-Charge</label>
<input type="text" class="form-control" id="OfficerInCharge" />
<label class="control-label">Contact Number</label>
<input type="text" class="form-control" id="ContactNumber" />
</div>
<div class="modal-footer">
<input id="submit" type="submit" value="SUBMIT" class="btn" />
</div>
</form>
我如何能够将表单中的数据插入到json文件中?
答案 0 :(得分:1)
首先,形式:&#34;保存更改&#34;按钮必须位于<form>
标记内。
<form>....
<input type="submit" value="Save changes">
</form>
第二步:在表单内部,您必须添加一些字段。
<form>
Name: <input type="text" name="name"><br>
Address: <input type="text" name="address"><br>
...
</form>
第三步:宣布&#34;行动&#34;和&#34;方法&#34;在你的表格上。
<form action="" method="post">
所以最后表格会是这样的:
<form action="" method="post">
Name: <input type="text" name="name"><br>
Address: <input type="text" name="address"><br>
<input type="submit" value="Save changes">
</form>
保存输入字段
<?php
include "lib/lib.php";
$url = "http://10.0.0.1/lib/api/desk/";
$params = array ("action" => "list","company_key" => "1");
$result=requestURL($url,$params);
$json_a=(array)json_decode(strip_tags($result));
$newdata=array();
foreach($_POST as $key=>$value) {
$newdata[$key]=$value;
}
$json_a['data'][]=$newdata;
$json_a=json_encode($json_a);
?>
之后,您将拥有一个包含新数据的JSON对象。我想你必须把它保存在某个地方。