0

I'm newbie of php and html and I've a problem. I have this form to upload a file :

    <h2>Upload file</h2>                
    <form name="uploadFile" action = "upload.php" method = "POST" enctype = "multipart/form-data">
    <input type = "file" name = "document"/>
    <input type = "submit"/>

In my external upload.php file if the upload is successful I echo a string with the name of the file and a message. If I show the value with the echo function the value is right, but I don't want a redirect to upload.php page, I want to show it back in HTML.

How can I print the return value of my upload.php file in HTML form page?

4
  • 1
    If you do not want to redirect the user to another page you can handle the POSTdata in the current script. To check if the form is submitted use an if like this: if(isset($_POST['document'])) Commented May 21, 2017 at 9:06
  • 2
    Use ajax and return the response from upload.php to show it on html page Commented May 21, 2017 at 9:07
  • how can I? can you please provide code or example? Commented May 21, 2017 at 9:21
  • Whenever you ask anything in the comments. Use @name_of_person .Otherwise he/she will not get notification of comments. Now your problem is solved or you want me to write code ? Commented May 21, 2017 at 10:32

4 Answers 4

2

Please change your index.php file as shown below :

<?php
      if(isset($_FILES['document'])) {
        echo $_FILES["document"]["name"];
        echo "<p id='results'></p>";
?>
<script>
        $(document).ready(function() {
            $.ajax({
                url: "data.json",
                dataType: "text",
                success: function(data) {
                    $('#results').html(data);
               }
           });
        });
</script>

<?php } ?>
<h2>Upload file</h2>                
<form name="uploadFile" method = "POST" enctype = "multipart/form-data">
    <input type = "file" name = "document"/>
    <input type = "submit"/>
</form>

This prints the filename of the file you uploaded.
When you work with file upload, PHP saves name, file size, and all other attribute to $_FILES variable which is a Global Variable.

As you said you want to read other file as soon as file uploaded, this might help.

Sign up to request clarification or add additional context in comments.

6 Comments

yes, It works thanks. I need also to return back another information from the upload.php external script and not only filename. Any idea?
a string there is content in the document.
If this answer helps, mark it as an answer, and also upvote it, so that others with the same problem can benefit in future.
you mean a string that the uploaded file contains?
|
0

You can redirect to the php page with GET variable like this: upload.php?status=success And then you can check for the status variable

8 Comments

Not downvoting but this doesn't help.
can you please example?
after finishing uploading you redirect to upload.php right? make a redirect to upload.php?status=success instead. and then you can check the status like this $status= isset($_GET['status']) ? $_GET['status'] : "noStatus";
@RST , i have used it before in collage projects :) and it works when i need to get status after doing a request
As far as I understand his scenario, his upload.php is where is determined whether or not the upload was successful. You are already sending 'success'. If you can add some code to explain what you are doing, it might help.
|
0

Add your php code for uploading at the top of the file where your form is and name it as .PHP file. Please try the below code,

<?php
  if(isset($_POST['document'])) {
    //do your post actions here
    //validate your input
    //upload the file
    //print success or error message
  }
?>
<h2>Upload file</h2>                
<form name="uploadFile" method = "POST" enctype = "multipart/form-data">
  <input type = "file" name = "document"/>
  <input type = "submit"/>

6 Comments

when your action attribute have upload.php, You'll never be back here with $_POST['document'] not null, It will always redirect to upload.php on submit.
@NiravMadariya, assume that the file itself upload.php or I will have the action empty. I will correct my answer. Sorry, I missed it. Thanks for the catch.
how in my index.php (where the form is) I show the return data of upload.php form action? In the php if code you posted, how can I print the return data from the upload.php?
@pnappa, I just corrected my answer based on NiravMadariya's comment. Removed the action attribute to post it to the same page.
I removed the action attribute, and I add php. The upload works, the page is still the same (without redirect) but the data is not shown. What I have to echo, the $document variable in the php code?
|
-1

As Nirav's posted an answer. Use it with some modification.

<?php
if(isset($_FILES['document'])) {
    echo $_FILES["document"]["name"];
    // when uploaded the file assign status=1;
    $file_name=$_FILES["document"]["name"];
    $status="1";
}
?>
<script type="text/javascript">
var status = <?php echo $status; ?>;
var filename = <?php echo $file_name; ?>;
$(document).ready(function(){
    if(status){
        $.ajax({
        url: "upload.php",
        type: "POST",
        data: {
            status:status,
            filename: filename
        }
        success: function(result){
                    $('#element').html(result);
                }
        });
    }
    else{
        $('#element').html("failed"); 
    }

});
</script>

HTML

<h2>Upload file</h2>                
<form name="uploadFile" action="upload.php" method = "POST" enctype = "multipart/form-data">
  <input type = "file" name = "document"/>
  <input type = "submit"/>
</form>
<div id="element"><!--Here Show message from upload.php--></div>

Upload.php

<?php
if(isset($_POST['status']) && !empty($_POST['status'])){

    echo $_POST['filename']." uploaded successfully";
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.