Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP File Download Using Hidden IFrame Without Saving
    primarykey
    data
    text
    <p>I apologize in advance for the confusing question wording, but I couldn't figure out how to put this.</p> <p>I essentially have a string in a database that I intend to make available for user download. How would I go about doing this? </p> <p>I was attempting to use ajax, but I wasn't sure how to go about it. </p> <p>The following jquery code is executed when the download link is pressed</p> <pre><code>$.ajax({ url: 'index.php/script/downloadFile', type: 'post', data: {name: $(this).text()} }); </code></pre> <p>The relevant PHP code looks like: </p> <pre><code>public function downloadScript(){ $name = $_POST['name']; $filename = $name . ".txt"; $string = //String that comes from database using name to create query $filename = $name . ".txt"; header('Content-type: text/plain'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header("Content-Length: " . strlen($string)); header("Connection: close"); echo $string; } </code></pre> <p>Essentially, I want the string to be there for a user to download as a text file. I didn't know if AJAX was the right way to do this or if there was a better way to do the same task. (I was assuming the better way would be to incorporate a hidden iframe somehow) </p> <p>For a little more information, I have a list of elements:</p> <p><li> John </li> <li> Jeff </li> <li> Joe </li> <li> Jack </li></p> <p>When one is clicked, I want to send the text of that element's name to the php file to form the query. </p> <p>That's why I don't have a simple href setup to start the file download. </p> <p><strong>EDIT:</strong></p> <p>While I'm at it, is there anyway for me to get more than one of these files, say there are multiple strings that are returned from the database, save them as separate text files, zip them up, and send them to the user? Or, would that require saving to the server? </p> <p>Any help is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload