John Doe
John Doe

Reputation: 10203

Upload a file using php script and open html page wit javascript using uploaded file (name)

I've created a PHP page to upload a file to the server.
I've created a HTML page with javascript that parses a file on the server.

I'd like to upload the file and after a successful upload directly open the HTML page and use the (uploaded) file name as parameter for my javascript.

How to combine these two. I can't get it working.

Upvotes: 0

Views: 246

Answers (1)

Kyle Richter
Kyle Richter

Reputation: 406

Umm, there's very little information here, but one way would be to render out your html with a hidden field where the value of the field was the filename. Then you could call getElementById or whatever method you use to find selectors to read that in, then use that in your javascript.

<html>
    <body>
        <span id="submittedFilename" style="display: none"><%php $filename %></span>
    </body>
</html>

<script>
    var filename = $('submittedFilename").html();
</script>

Another option would be to set a variable into your rendered php (html) page, and then use that in your javascript.

<script>
    var filename = <%php $filename %>;
</script>

================ EDIT ==========================

<%php // This is your processing script 
     ... Your processing logic ...

     // After processing, add a variable called filename to session.
     session_start();
     $_SESSION['filename'] = $filename;

     header('Location: /newPhpScript.php');

%>

-- Then your new php script could look something like this:

<%php session_start() %>
<html>
    <body>
        <script>
           // if using jQuery
            var filename = $('#filename').text();
           // else use this
            var filename = document.getElementById('filename').innerHTML;
        </script>
        <span id="filename" style="display: none"><%php echo $_SESSION['filename'] %></span>
    </body>
</html>

Upvotes: 1

Related Questions