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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.