简体   繁体   中英

Staying on the same page PHP and HTML

I am trying to submit a form and after submission, the webpage does not go to a different page. Here are some of the resources that I've looked at: php form - on sumbit stay on same page and php and html form on the same page . However, none of their solutions seemed to fully work, namely, it helped me figure out how to stay on the same page by putting the PHP code in the same file as the HTML but it is not "echoing" any message on the website so I'm not sure if the PHP is actually working. So here is my code:

<!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>

After uploading a file and submitting it, the webpage does not go to a different page. However, it is not echoing yo! what up homie?! yo! what up homie?! . Any suggestions on how to stay on the same page AND echo the message after the user presses the submit button?

<!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>

or better write only

<?php phpinfo(); ?>

in your file. if nothing shows up you don't have PHP

if so you can try things like:

<?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>

keep your form inside else part, and your file should be in .php

see DEMO here,

<!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM