GeorgeButter
GeorgeButter

Reputation: 2589

Submitting formatted JSON with PHP form

Files

index.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
// 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:

{"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"}
 }

Upvotes: 3

Views: 28880

Answers (2)

raidenace
raidenace

Reputation: 12836

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

Upvotes: 9

Philipp
Philipp

Reputation: 15629

You know json_encode? Simply pass an assisiative array to this function with the given structure

Create an new array i.e and pass it to json_encode

$array = array(
    "startDate" => $_POST['startDate'],
    //..
    "assets" => array(
            "media" => $_POST["media"]
            //..
    )
);
$json = json_encode($array);

Upvotes: 1

Related Questions