PHP / HTML 表单 - 在 Docker 容器中的同一页面上显示数据,不打印

Bri*_*own 4 html php apache docker

我想在 Apache 上有一个 HTML/PHP 简单表单。我希望用户提交一些数据,然后这些数据应该在提交后显示在同一页面上。我想将所有内容都放在 Docker 容器中。

这可能是 Docker 问题,因为它在localhost上运行。

我的Ubuntu系统上安装了 PHP。但是,提交表单后,我看不到我提供的数据。

文件索引.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>
Run Code Online (Sandbox Code Playgroud)

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"]
Run Code Online (Sandbox Code Playgroud)

Abd*_*lam 5

错误

  1. 当文件名以.html. 它应该是index.php

  2. 您正在使用已包含 Apache 和 PHP 的 PHP 映像webdevops/php:8.2

    据我所知,这些行不通:

    RUN apt install -y apache2
    RUN apt install -y apache2-utils
    RUN sudo a2enmod php8.2
    
    Run Code Online (Sandbox Code Playgroud)
  3. 您正在将文件复制到名为的目录中,post该目录不存在且似乎不需要


尝试这个:

文件索引.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>
Run Code Online (Sandbox Code Playgroud)

Dockerfile

FROM webdevops/php-apache:8.2

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

EXPOSE 80
CMD ["supervisord"]
Run Code Online (Sandbox Code Playgroud)