Johnson Doe Johnson Doe - 1 year ago 68
Ajax Question

Sending data through post to php then downloading

I have a theme building website that creates a theme for another project which isn't relevant. I have a few different inputs that have data in them. I am able to get the data and store them in variables. When I click the download button I want the variables to send to a .php file and then download that file. I believe I am sending it ok, but downloading the data seems to be the difficult part.

$("#download-theme").click(function() {
$(function() {
type: "POST",
url: 'download.php',
data: ({
value: value
success: function(data) {

I believe that this sends it to the download.php file as I get the alert with the correct value. What I want to do next is to download the data of the php file as a .txt file. Maybe i'm over complicating it by sending it to the php file i'm not sure.

All in all, I want to get the data from the variable put it in a file, and have the user download it.

EDIT: I saw quentino response and it works, but I have 5 inputs that I would need to be transferred by one submit button.

<input type="text" name="variable1" />
<input type="text" name="variable2" />
<input type="text" name="variable3" />
<input type="text" name="variable4" />
<input type="text" name="variable5" />
<input type="submit" value="send"/>

Answer Source

Are you sure you must use ajax? You can do that using hidden form, set data in onclick event and send it. Simple one file example:

if (isset($_POST['data'])) {
    header("Content-disposition: attachment; filename=\"test.txt.\"");
    echo $_POST['data'];
} else {
        <form method="post"/>
            <input type="text" name="data" />
            <input type="submit" value="send"/>
<?php } ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download