So i have an HTML form which on click of ‘submit’ posts input data through to a basic PHP file which takes the data and stores it in a JSON file. I am able to run the index.html file on a PHP webserver with the command php -S localhost:4040 and on submit, it does as expected. i want to containerise the website. But when I run the website on a docker container, click submit on the form, I get shown ‘405 Not Allowed’ error. i use docker-compose.yml file to get the docker container running. How can I get this working on a docker container?
Snippet of my code.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Registration</title>
<link rel="stylesheet" href="css/styles.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
</head>
<body class="container">
<div class="container">
<h1>Registration</h1>
<section>
<form id="regForm" action="form.php" method="post">
<div class="form-group">
<label for="nameInput">Name</label>
<input type="text" class="form-control" id="nameInput" name="studentName" aria-describedby="emailHelp" placeholder="Enter your full name">
</div>
<div class="form-group">
<label for="loginInput">Login</label>
<input type="text" class="form-control" id="loginInput" name="studentId" placeholder="Enter your login">
</div>
<button type="submit" name="submit" value="submit" class="btn btn-primary" id="submitForm">Submit</button>
</form>
</section>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
PHP
<?php
error_reporting(0);
echo "<h2>Submitted</h2>";
if(isset($_REQUEST['submit'])) {
$data = '';
$filename = "data.json";
//if file exists
if(is_file($filename)) {
$data = file_get_contents($filename);
}
$json_arr = json_decode($data,true);
$json_arr[] = array(
'studentId' => $_REQUEST['studentId'],
'studentName' => $_REQUEST['studentName'],
);
//saving file in jsonarray
file_put_contents($filename, json_encode($json_arr));
}
?>
docker-compose.yml
version: '3'
services:
product-services:
build: ./src
volumes:
- ./src:/usr/share/nginx/html
ports:
- 5001:80