I am trying to create a webpage where the user can input a username and ID and it updates a .json file with the provided input, but whenever I press the submit button, it redirects me to a blank page (i.e. example.com/index.php), and it doesn’t update the JSON file.
I’ve tried:
- Running the website locally on my PC
- Updating example.com.conf file to run php through /var/run/php/php7.4-fpm.sock
I am using nginx version 1.18.0 on a Raspberry Pi 3B+
index.html, index.php and whitelist.json are all in the same directory.
HTML Code (index.html):
<form method="post" action="index.php">
<div class="group">
<input type="text" name="username" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Username</label>
</div>
<div class="group">
<input type="text" name="uuid" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>UUID</label>
</div>
<button id="btn"> <p id="btnText">Submit</p> </button>
</form>
PHP Code (index.php):
<?php
$data = file_get_contents('whitelist.json');
$json_arr = json_decode($data, true);
$json_arr[] = array('uuid'=>$_POST['uuid'], 'name'=>>$_POST['username']);
file_put_contents('whitelist.json', json_encode($json_arr));
?>
2
Answers
Firstly, this
'name'=>>$_POST['username']
should be'name'=>$_POST['username']
(You have an extra>
)And secondly, I would rename the
index.php
file to something else likesubmit.php
so it is not conflicting (don’t forget to also changeaction="index.php"
to the new file name)If you do those two things the
whitelist.json
should update with the inputted values (but you will still have the blank page problem as described by me and @Barmar in the comments)OR instead of having two separate index’s for HTML & PHP, you could just have one file
index.php
, for both (what I recommend); and then you can create yourwhitelist.json
in the same directory.index.php Example: