My php script doesn't append to my Json file. What am I doing wrong?

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!

Hi!

Welcome to the forum :partying_face:! Now, for your problem…

In regards to your HTML form, that’s exactly how you have it on your page? If so, the form tag is missing an action attribute, which is used to tell the browser where to send the form data.

If you had two files on the root folder of your project, index.html and save.php, then your form should be:

<form method="post" action="save.php">
  <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>

Other than that, your script is working fine (I tested it). Be advised that once your script (save.php) finished storing the data a blank page will be shown, unless you’re sending the data using AJAX or define a redirect to another page.

Hope it helps :slight_smile:,

Happy coding!