I am working with a simple PHP script that parses data filled in simple HTML forms to a JSON file. The problem is that I can't make all the added objects to appear inside the same array like this:
{
"arraynamefoo": [
{
"name": "Testing¹",
"latitude": "32.75039432",
"longitude": "-117.01482831",
"description": "Testing, first description...",
"category": "park",
"photourl": "http://www.availableideas.com/wp-content/uploads/2016/10/Colorful-Polygonal-Render-iPhone-6-Plus-HD-Wallpaper.jpg"
},
{
"name": "Testing²",
"latitude": "32.7896552",
"longitude": "-117.1686369",
"description": "Testing, second description. . .",
"category": "park",
"photourl": "http://1.bp.blogspot.com/-olGHQWTmk3A/T0tZVSU3CDI/AAAAAAAABbA/EgjurSou6_Q/s1600/12.jpg"
},
{
"name": "Testing³",
"latitude": "32.89119111",
"longitude": "-116.86513959",
"description": "Testing, third description.",
"category": "park",
"photourl": "http://getwallpapers.com/wallpaper/full/c/f/0/674534.jpg"
}
]
}
I couldn't even set the given array name in the PHP script, so I can't neither be able to make that all the written data belongs into the same JSON array... So, someone can please help me pointing what I need to to modify in the PHP code to make this possible? Please and thanks in advance.
The write.php
file:
<?php
$filetxt = 'file.json';
if(isset($_POST['name']) && isset($_POST['latitude']) && isset($_POST['longitude']) && isset($_POST['description']) && isset($_POST['category']) && isset($_POST['photourl'])) {
if(empty($_POST['name']) || empty($_POST['latitude']) || empty($_POST['longitude']) || empty($_POST['description']) || empty($_POST['category']) || empty($_POST['photourl'])) {
echo 'You need to fill all the fields';
}
else {
$data = array(
'name'=> $_POST['name'],
'latitude'=> $_POST['latitude'],
'longitude'=> $_POST['longitude'],
'description'=> $_POST['description'],
'category'=> $_POST['category'],
'photourl'=> $_POST['photourl'],
);
$filetxt = 'file.json';
$arr_data = array();
if(file_exists($filetxt)) {
$jsondata = file_get_contents($filetxt);
$arr_data = json_decode($jsondata, true);
}
$arr_data[] = $data;
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
if(file_put_contents('file.json', $jsondata)) echo 'Successfully sent data!';
else echo 'Error while attempting to write to JSON';
}
}
else echo 'Error writing data to JSON!';
?>
Also, the write.html
file (maybe it's not needed to the question, but it's here anyway):
<form action="write.php" method="post">
Name: <input type="text" name="name" id="name" /></br>
Latitude: <input type="text" name="latitude" id="latitude" /></br>
Longitude: <input type="text" name="longitude" id="longitude" /></br>
Description: <input type="text" name="description" id="description" /></br>
Category: <input type="text" name="category" id="category" /></br>
Photo URL: <input type="text" name="photourl" id="photourl" /></br>
<input type="submit" id="submit" value="Submit" />
I've rewritten your code below and added some comments to explain what I'm doing.
<?php
// Set constants as such
const FILEPATH = 'file.json';
const FIELDS = [
'name',
'latitude',
'longitude',
'description',
'category',
'photourl'
];
// Loop over the fields you require rather than typing them out (so much less work!)
foreach (FIELDS as $fieldName) {
if (!array_key_exists($fieldName, $_POST) || strlen($_POST[$fieldName]) <= 0) {
echo 'You need to fill all the fields.';
die();
}
}
$arr_data = [
'arraynamefoo' => []
];
if (file_exists(FILEPATH)) {
$jsonString = file_get_contents(FILEPATH);
$arr_data = json_decode($jsonString, true);
}
// arraynamefoo is the name you showed in your pastebin.
// Adding the trailing [] will append the contents of $_POST to 'arraynamefoo'
$arr_data['arraynamefoo'][] = $_POST;
$jsonString = json_encode($arr_data, JSON_PRETTY_PRINT);
if (file_put_contents('file.json', $jsonString)) {
echo 'Successfully sent data!';
die();
}
echo 'Error while attempting to write to JSON';