2

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?!. Any suggestions on how to stay on the same page AND echo the message after the user presses the submit button?

Community
  • 1
  • 1
Jae Kim
  • 625
  • 3
  • 12
  • 23

2 Answers2

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

Julio Soares
  • 1,200
  • 8
  • 11
  • This answer kinda helps. But I would like the message to show up AFTER the user clicks the submit button. In other words, I would like a set of PHP code to be executed after the user clicks on the submit button. – Jae Kim Oct 08 '15 at 12:53
  • So... since you have php... php runs on the server.the page you see in in the client (your browser) php code will run before each page is sent to you. When you post, the infor you've posted is received by php at the server, it will deal with it and maybe send something back. – Julio Soares Oct 08 '15 at 12:57
  • Your code doesn't work for me... I have no idea why... I mean it looks good to me... That is, nothing gets echoed... – Jae Kim Oct 08 '15 at 13:05
  • that would be a miracle... are you putting the file in the right palce in the server and looking for the right file on your browser? – Julio Soares Oct 08 '15 at 13:10
0

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>
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41