skip to Main Content

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


  1. 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

    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"]
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search