Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple php code which i embedded inside my html code. I want the code to run only when a button is clicked. But when the page loads automatically the function is called. The code will explain you clearly.

<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<script>
function exportcsv()
{
<?php exportcsv(); ?>;
}
</script>
<?php 
 function exportcsv()
 {
 $File = "YourFile.txt"; 
 $Handle = fopen($File, 'w');
 $Data = "Visual BI Solutions\n"; 
 fwrite($Handle, $Data); 
 $Data = "ajay,praveen,musthafa,sanjay"; 
 fwrite($Handle, $Data); 
 fclose($Handle); 
 }
 ?>
 <button onClick="exporttxt()">Export</button>

How to make it run only when the button is clicked.??

share|improve this question

closed as too broad by Marc B, charlietfl, andrewsi, kumar_v, Tom Fenech Apr 8 at 11:30

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

5  
PHP runs on server side, so it runs before/while output your page to client. If you need to run PHP code in response of button click (or whatever client side event), use AJAX. –  Passerby Nov 5 '12 at 4:56
2  
Use Ajax for this purpose –  Engr. Umair Aziz Attari Nov 5 '12 at 4:57
    
Any other go without using ajax? –  Praveen Singh Nov 5 '12 at 4:59
1  
@praveensingh: In that case you need to submit the whole page to the server which will refresh your page. What's wrong with using AJAX? –  Sayem Ahmed Nov 5 '12 at 5:01
1  
AJAX tutorials are a dime a dozen... make google your friend –  charlietfl Nov 5 '12 at 5:11

2 Answers 2

You can use AJAX for getting what you want.. PHP functions run before your browser loads the page because its SERVER side and any user action is user side where you can use javascript-ajax calls to add what you need..

http://api.jquery.com/jQuery.ajax/

Here is the jQuery ajax .. (you need to add jquery lib so you can use it). (at you html HEAD one script tag with source the jquery lib)..

It will make a request inside your browser when your user click on the button and it load the responde at his browser. Example..

<script>
$(document).ready(function(){
    $("#buttonID").click(function(){
        $.ajax({
           url: "script.php",
           type: "POST",
           data: {action : 'exportcsv'},
           dataType: "text",
           success: function(responde){
               alert(responde); /*This will show to user as message the responde*/
           }
        });
    });
});
</script>

At your php you must make PAGE(in my case script.php) or part of page will responde only for that ajax call.. that means that it wont display any other information except the answer of that call..

function exportcsv()
 {
 $File = "YourFile.txt"; 
 $Handle = fopen($File, 'w');
 $Data = "Visual BI Solutions\n"; 
 fwrite($Handle, $Data); 
 $Data = "ajay,praveen,musthafa,sanjay"; 
 fwrite($Handle, $Data); 
 fclose($Handle); 
 }
if(isset($_POST['action']) && $_POST['action']=='exportcsv'){
    exportcsv(); 
    echo "SUCCESS";
    exit(); /*To prevent outputing other data..*/
}

You can set it at your main php file but it must be at the first lines of your page so there are no other outputs before it. The exit() will prevent all next php code to proccess.

share|improve this answer

If you don't want to use AJAX, you can just have the button post to the same page. You'll get a page refresh, but the function won't launch until the button is pressed.

<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<?php 
 function exportcsv()
 {  
 $File = "YourFile.txt"; 
 $Handle = fopen($File, 'w');
 $Data = "Visual BI Solutions\n"; 
 fwrite($Handle, $Data); 
 $Data = "ajay,praveen,musthafa,sanjay"; 
 fwrite($Handle, $Data); 
 fclose($Handle); 
 }
 if(isset($_POST['export'])) exportcsv();
 ?>
 <form method="post">
 <input type="submit" name="export" value="Export" />
 </form>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.