Hello,
I’m making a website for a local sportsclub but I have some trouble with putting form inputs into a Json file. I watched this simple youtube video about how to do it but with no success. This is my code:
form:
<form method="post">
<div class="form-group">
<label for="exampleFormControlInput1">titel</label>
<input type="text" name="titel" class="form-control" id="titel" placeholder="">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Beschrijving</label>
<textarea class="form-control" name="beschrijving" id="beschrijving" rows="3"></textarea>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Datum</label>
<input type="text" name="datum" class="form-control" id="datum" placeholder="">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Eventuele afbeelding</label>
<input type="text" name="foto" class="form-control" id="foto" placeholder="Bijvoorbeeld: https://koken.medialaancdn.be/sites/koken.vtm.be/files/recipe/image/2016/12/mosselenfrietklein.jpg">
</div>
<div class="form-group">
<input type="submit" name="submit" value="Bericht plaatsen" />
</div>
</form>
My php script:
<?php
$message = '';
$error = '';
if(isset($_POST["submit"]))
{
if(file_exists('json/nieuws.json'))
{
$current_data = file_get_contents('json/nieuws.json');
$array_data = json_decode($current_data, true);
$extra = array(
'titel' => $_POST['titel'],
'beschrijving' => $_POST["beschrijving"],
'datum' => $_POST["datum"] ,
'foto' => $_POST["foto"]
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('json/nieuws.json', $final_data))
{
$message="<label class='text-success'>File Appended Success fully</p>";
}
}
else
{
echo('JSON File not exits');
}
}
?>
I don’t have a lot of experience with PHP but from my understanding this code should work but it doesn’t. Any help is appreciated.
Thanks for reading!