I want to have HTML/PHP simple form on Apache. I would like the user to submit some data and then this data should be shown on the same page after submission. I want to have it all in a Docker container.
It might be the Docker issue, because it works on localhost
I have php installed on my Ubuntu. However, after submitting a form, I can’t see the data I provided.
index.html
<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
Dockerfile
FROM webdevops/php:8.2
RUN apt update -y && apt upgrade -y
RUN apt install -y apache2
RUN apt install -y apache2-utils
RUN sudo a2enmod php8.2
WORKDIR /var/www/html
EXPOSE 80
COPY index.php /var/www/html/post
CMD ["apache2ctl", "-D", "FOREGROUND"]
2
Answers
Errors
PHP won’t execute when the file name ends with
.html
. It should beindex.php
You’re using the PHP image of
webdevops/php:8.2
which already has Apache and PHP.As I know these will not work
You’re copying your file into a directory named
post
which doesn’t exist and doesn’t seem neededtry this
index.php
Dockerfile
I see that in your form tag, the action attribute is empty (
action=""
). This will submit to the same URL it’s currently on.Change it to
action="index.php"
to submit the form data to the PHP script.