Theo Ymca
Theo Ymca

Reputation: 119

Post some value in HTML to a PHP?

How can I post some value in HTML to a PHP?

Here is my situation :

  1. in Download.php, i am going to Export a Excel in csv format.

  2. the data to Export are from a editable table, let say in myTable.php. and those data I am not going to store them in the data Base, I just want to let the user download a Excel version of their edit.

I can put my data into javascript, and use ajax to post a form, but if then, I don't know how to handle the result to make it work. as "Download.php" is create the file by setting the header like

header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=sample.csv');

and that is not working in a ajax call feed back.

if I am not using Ajax, how can I put data like $('#mainTable').html() into a hidden input or something ?

Upvotes: 0

Views: 84

Answers (2)

Konsole
Konsole

Reputation: 3475

You cannot download file directly using ajax. If you want to make a download work using Ajax you can return download url in response and apply it like document.location = "your_download_url" to start download process.

EG

someObject.click(function(){
       $.ajax({...,
       success:function(download_url_from_server){
           document.location = download_url_from_server;
           //eg of download url: http://yoursite.com/download.php?file=yourfile
       }});           
    })

Upvotes: 0

crafter
crafter

Reputation: 6297

If you are rendering your results as a download (for example a spreadsheet), then don't use ajax.

The header() function asks the browser to act in a certain manner, in this case render the requested page as a download. In the case of unit, you forgo this, as you take control of output.

Therefore, a plain

<form ...action="Dowload.php">
...
</form>

will suffice.

Upvotes: 2

Related Questions