php - display data of form in html

351

I have a form in HTML. After submitting it, I want to show the name of file in the same HTML page. I use a PHP file calledupLoad.php to echo the name of the file but what should I do to show the name of file in the same html page? I also don't want to use php code in my html code. HTML:

<form action="upLoad.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" name="submit" value="submit"/>
</form>

PHP:

if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
218

Answer

Solution:

Keepaction empty

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" name="submit" value="submit"/>
</form>

And put this at the start of the page

if(isset($_POST['submit']))
{
  if ($_FILES["file"]["error"] > 0)
  {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
  else
  {
    $print  = "Upload: " . $_FILES["file"]["name"] . "<br />";
    $print .= "Type: " . $_FILES["file"]["type"] . "<br />";
    $print .= "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    $print .= "Stored in: " . $_FILES["file"]["tmp_name"];
  }
}

You can now use the$print variable anywhere in the same page to show the result by usingecho $print

415

Answer

Solution:

You cannot receive data with HTML only so you'll have to make your first page a .php with:

<?php
if(isset($_FILES))
{
  if ($_FILES["file"]["error"] > 0)
  {
     echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
  else
  {
     echo "Upload: " . $_FILES["file"]["name"] . "<br />";
     echo "Type: " . $_FILES["file"]["type"] . "<br />";
     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
     echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
}
?>
<form action="thispage.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" name="submit" value="submit"/>
</form>

And if you end up working with multiple forms you can use a get variable on your action like this:

<?php 
if($_GET['delete'] == 1)
{
 //PHP code
}
elseif($_GET['update'] == 1)
{
 //PHP code
}
?>

<form action='thispage.html?delete=1' method='POST' >
 <!-- html inputs -->
</form>

<form action='thispage.html?update=1' method='POST' >
 <!-- html inputs -->
</form>

Or just use an hidden input if you prefer not to have any GET variables like so:

<?php 
if($_POST['delete'] == 1)
{
 //PHP code
}
elseif($_POST['update'] == 1)
{
 //PHP code
}
?>

<form action='thispage.html' method='POST' >
 <input type='hidden' name='delete' value='1' />
 <!-- html inputs -->
</form>

<form action='thispage.html' method='POST' >
 <input type='hidden' name='update' value='1' />
 <!-- html inputs -->
</form>
533

Answer

Solution:

If you really don't want to use php on the HTML file, you could use jquery form plugin http://jquery.malsup.com/form/

People are also looking for solutions to the problem: PHP sleep versus Cron-job

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.