skip to Main Content

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


  1. 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 like submit.php so it is not conflicting (don’t forget to also change action="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 your whitelist.json in the same directory.

    index.php Example:

    <?php
        # check if submit button was pressed and make sure both textboxes aren't empty
        if(isset($_POST['submit']) && !empty($_POST['uuid']) && !empty($_POST['username'])){
            
            # declare file path & name for .json file 
            $filepath = "whitelist.json";
    
            # check if it exists 
            if(file_exists($filepath)){
                # if it does, get contents and decode into array
                $data = file_get_contents($filepath);
                $json_arr = json_decode($data, true);
            }
    
            # add to existing array from above or create a new one with submitted data
            $json_arr[] = array('uuid' => $_POST['uuid'], 'name' => $_POST['username']);
    
            # open or create file & truncate it.
            $fopen = fopen($filepath, "w");
    
            # try to write data to said file; return $output regarding result
            if($b = fwrite($fopen, json_encode($json_arr, JSON_PRETTY_PRINT))) $output = "Success! ${b} bytes written to: ${filepath}";
            else $output = "Error!";
    
            # close open file/handle
            fclose($fopen);
        }
    ?>
    
    <html>
        <head>
            <title>Create ID</title>
        </head>
        <body>
            <h2>Create ID</h2>
            <form action="" method="POST" enctype="multipart/form-data">
                <input required type="text" name="uuid" placeholder="UUID"><br>
                <input required type="text" name="username" placeholder="Username">
                <input type="submit" name="submit" value="Submit">
            </form>
    
            <!-- echo $output only if declared and different than NULL -->
            <span><?php if(isset($output)) echo $output; ?></span>
        </body>
    </html>
    
    Login or Signup to reply.
  2. <?php
    // router.php
    if (preg_match('/.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
        return false;    // serve the requested resource as-is.
    } else { 
        echo "<p>Welcome to PHP</p>";
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search