使用 PHP 表单提交格式化的 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12643152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submitting formatted JSON with PHP form
提问by Buts
Files
文件
index.php
索引.php
<form name="postform" action="post.php" method="post" enctype="multipart/form-data">
<table class="postarea" id="postarea">
<tbody>
<tr> <td class="postblock">Date:</td><td><input type="text" name="startDate"></td></tr>
<tr> <td class="postblock">Title:</td><td><input type="text" name="headline"></td></tr>
<tr> <td class="postblock">Article:</td><td><textarea id="text" rows="5" cols="30" type="text" name="text"></textarea> </td> </tr>
<tr> <td class="assetblock">Image address:</td><td><input type="text" name="media"></td></tr>
<tr> <td class="assetblock">Image caption:</td><td><input type="text" name="caption"></td></tr>
<tr> <td class="postblock"></td><td> <input type="submit" value="Submit Entry"> </td> </tr>
</tbody>
</table>
</form>
</form>
post.php
后.php
<?php
// check if a form was submitted
if( !empty( $_POST ) ){
// convert form data to json format
$json = json_encode( $_POST );
// make sure there were no problems
//if( json_last_error() != JSON_ERROR_NONE ){
//exit; // do your error handling here instead of exiting
// }
$file = 'entries.json';
// write to file
// note: _server_ path, NOT "web address (url)"!
file_put_contents( $file, $json, FILE_APPEND);
}
Question
题
I am trying to create a form that will allow my users to add entries to the site. Entries are stored in a JSON file. I have created a form that submits to the JSON form and is formatted like this:
我正在尝试创建一个表单,允许我的用户向站点添加条目。条目存储在 JSON 文件中。我创建了一个提交到 JSON 表单的表单,其格式如下:
{"startDate":"example",
"headline":"example",
"text":"example",
"media":"example",
"caption":"example"}
Media and caption relate to an image, video or other multimedia, i need them to be stored as a separate object like the example below.
媒体和标题与图像、视频或其他多媒体有关,我需要将它们存储为单独的对象,如下例所示。
{"startDate":"example",
"headline":"example",
"text":"example",
"asset" :{"media":"example",
"caption":"example"}
}
回答by raidenace
<?php
// check if a form was submitted
if( !empty( $_POST ) ){
// convert form data to json format
$postArray = array(
"startDate" => $_POST['startDate'],
"headline" => $_POST['headline'],
"text" => $_POST['text'],
"asset" => array(
"media" => $_POST["media"],
"caption" => $_POST['caption']
)
); //you might need to process any other post fields you have..
$json = json_encode( $postArray );
// make sure there were no problems
//if( json_last_error() != JSON_ERROR_NONE ){
//exit; // do your error handling here instead of exiting
// }
$file = 'entries.json';
// write to file
// note: _server_ path, NOT "web address (url)"!
file_put_contents( $file, $json, FILE_APPEND);
}
回答by Philipp
You know json_encode? Simply pass an assisiative array to this function with the given structure
你知道json_encode吗?只需将具有给定结构的辅助数组传递给此函数
Create an new array i.e and pass it to json_encode
创建一个新数组 ie 并将其传递给 json_encode
$array = array(
"startDate" => $_POST['startDate'],
//..
"assets" => array(
"media" => $_POST["media"]
//..
)
);
$json = json_encode($array);

