保持在PHP和HTML的同一页面上


Staying on the same page PHP and HTML

我正在尝试提交表单,提交后,网页不转到不同的页面。以下是我看过的一些资源:php表单—在同一页面上,php和html表单在同一页面上。然而,他们的解决方案似乎都没有完全工作,也就是说,它帮助我弄清楚如何通过将PHP代码放在与HTML相同的文件中来保持在同一页面上,但它并没有"回应"网站上的任何消息,所以我不确定PHP是否实际工作。下面是我的代码:

<!DOCTYPE html>
<html>
<body>
<form method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
<?php
    if(isset($_POST['submit'])) 
    {
        echo "yo! what up homie?!";
    }
    else               
    {
        // Display the Form and the Submit Button
    }
?>
</body>
</html>

上传文件并提交后,网页不会转到另一个页面。但是,它不响应yo! what up homie?!。关于如何留在同一页面上,并在用户按下提交按钮后回复消息的任何建议?

<!DOCTYPE html>
<html>
<body>
<?php echo "<p>PHP is installed and working</p>"; ?>
<form method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

或者最好只写

<?php phpinfo(); ?>
文件中的

。如果没有显示,说明没有PHP

如果是的话,你可以尝试这样做:

<?php
if(isset($_POST['submit'])) 
{
    $resp = "yo! what up homie?!";
}
else               
{
    $resp = "you haven't submitted yet!";
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" enctype="multipart/form-data">
    Select image to upload: <?php echo $resp; ?>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

保持你的表单在else部分,你的文件应该在。php

参见DEMO,

<!DOCTYPE html>
    <html>
        <body>

           <?php
              if(isset($_POST['submit'])) 
              {
                  echo "yo! what up homie?!";
              }
               ?>
                 <form method="post" enctype="multipart/form-data">
                    Select image to upload:
                    <input type="file" name="fileToUpload" id="fileToUpload">
                    <input type="submit" value="Upload Image" name="submit">
                 </form>
           ?>
      </body>
</html>