Jump to content

Run a function on button press


duck__boy

Recommended Posts

Hey all,

 

I have a function that allows users to export data from a table to a .csv file. This was pretty simple and works ok, but I have two problems that I don't know how to solve.

 

First, how would I fire this script with a button, and second how (when the button is first pressed) do I bring up a box to allow the user to select where to save the file? I realise the second query is more related to client side as opposed to server, so I'm really just after pointer of what to look for on that one.

 

Any help would be appriciated.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/199360-run-a-function-on-button-press/
Share on other sites

First, how would I fire this script with a button

 

<form action="#" method="POST">
<?php

if (isset($_POST['some_button'])) {
  echo 'fired';
}

?>
<input type="submit" name="some_button" value="Click me!">
</form

 

do I bring up a box to allow the user to select where to save the file?

 

http://webdesign.about.com/od/php/ht/force_download.htm

Hey ignace, thanks for the reply.

 

I have now got my functions firing off of a button press, so thanks for that.

 

I'm still suffering from a lack of understanding about the other part though - First thing I do when the button is pressed is run a script that creates a temporary file on my server, and that works fine. Next I'm trying to use the help you provided to force the download of that data, and I have created a new file and placed the headers with correct names etc in the there, but when that file is called I get to following error -

 

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\intranet dev\wp-content\themes\assets\index_users.php:41) in C:\wamp\www\intranet dev\wp-content\themes\assets\functions\download_tempData-csv.php on line 2

 

 

index_users.php is where I'm trying to control this from, and download_tempData-csv.php is where I placed the headers (and nothing else, just the headers.). I'd very much appriciate it if you were able to help further.

 

Thanks.

Well delete any white space and the file is now being picked up, but using the code below the file that is saved contains the complete source of the page, rather than the file I wish to down load. Sorry to be a pain, I really have no experience of this at all though.

 

Thanks.

 

<?php
header('Content-type: text/plain');
header('Content-disposition: attachment; filename=c:\\tempData.csv');
readfile('c:\\tempData.csv');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.