如何从html表单将数据插入json

时间:2021-12-21 16:52:49

I was able to get the json file through this code:

我能够通过以下代码获取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)); 
?>

The result of that is:

结果是:

{
    "data": [
        {
            "id": "18",
            "name": "SM Quezon",
            "branch_address": "Quezon City, Philippines",
            "officer_in_charge": "Juan Dela Cruzz",
            "contact_number": "09321234567, 02-3449067"
        }
    ]
}

I have a form which should function to add data array to a json file. What should happen is that after submitting the form, the inserted data will now be included in json file.

我有一个表单应该用于将数据数组添加到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>

How will I be able to insert the data I have from the form to json file?

我如何能够将表单中的数据插入到json文件中?

1 个解决方案

#1


1  

First of all, the form: the "Save changes" button must be inside the <form> tag.

首先,表单:“保存更改”按钮必须位于

标记内。

    <form>....
    <input type="submit" value="Save changes">
    </form>

Second step: inside the form you must add some fields.

第二步:在表单内部,您必须添加一些字段。

<form>
Name: <input type="text" name="name"><br>
Address: <input type="text" name="address"><br>
...
</form>

Third step: declare "action" and "method" on your form.

第三步:在表单上声明“action”和“method”。

<form action="" method="post">

So at the end the form will be something like this:

所以在最后,表单将是这样的:

<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>

SAVE the INPUT FIELDS

保存输入字段

<?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);
?>

After that you will have a JSON object with included the new data. I suppose that you must save it somewhere.

之后,您将拥有一个包含新数据的JSON对象。我想你必须把它保存在某个地方。

#1


1  

First of all, the form: the "Save changes" button must be inside the <form> tag.

首先,表单:“保存更改”按钮必须位于

标记内。

    <form>....
    <input type="submit" value="Save changes">
    </form>

Second step: inside the form you must add some fields.

第二步:在表单内部,您必须添加一些字段。

<form>
Name: <input type="text" name="name"><br>
Address: <input type="text" name="address"><br>
...
</form>

Third step: declare "action" and "method" on your form.

第三步:在表单上声明“action”和“method”。

<form action="" method="post">

So at the end the form will be something like this:

所以在最后,表单将是这样的:

<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>

SAVE the INPUT FIELDS

保存输入字段

<?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);
?>

After that you will have a JSON object with included the new data. I suppose that you must save it somewhere.

之后,您将拥有一个包含新数据的JSON对象。我想你必须把它保存在某个地方。