3

I want to have an 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 system. However, after submitting a form, I can't see the data I provided.

File 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"]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian Brown
  • 3,873
  • 16
  • 48
  • 79

2 Answers2

5

Errors

  1. PHP won't execute when the file name ends with .html. It should be index.php

  2. You're using the PHP image of webdevops/php:8.2 which already has Apache and PHP.

    As I know, these will not work:

    RUN apt install -y apache2
    RUN apt install -y apache2-utils
    RUN sudo a2enmod php8.2
    
  3. You're copying your file into a directory named post which doesn't exist and doesn't seem needed


Try this:

File index.php

<?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-apache:8.2

WORKDIR /var/www/html
COPY index.php ./

EXPOSE 80
CMD ["supervisord"]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
-4

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.

Sreeram Nair
  • 2,369
  • 12
  • 27
  • Please add some more explanation to your answer such that others can learn from it. If this worked on localhost, but no longer when using Docker, what makes you think that your hint resolves the problem? Also, what makes you think that `index.php` exists after all? The OP hasn't shared any such file – Nico Haase Jul 06 '23 at 09:00
  • The docker file shared, says COPY index.php /var/www/html/post, so i think index.php is used – Sreeram Nair Jul 06 '23 at 09:05
  • Please add all clarification to your answer by editing it – Nico Haase Jul 06 '23 at 09:13
  • `index.php` will be the default page shown, so setting `action="index.php"` or leaving it blank wouldn't make any difference – DarkBee Jul 06 '23 at 09:27
  • 1
    Hi, Sreeram Nair. Most or all of your last 11 answers appear likely to be entirely or partially written by AI (e.g., ChatGPT). Many also have comments indicating that they are wrong in some respect. Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. We do hope you'll stick around and continue to be a valuable part of our community by posting *your own* quality content. Thanks! – NotTheDr01ds Jul 22 '23 at 22:31
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. – NotTheDr01ds Jul 22 '23 at 22:31