Call php function when pressing submit button

Hello. I have a function wich export a specific table into a CSV file. If i'm moving the code outside the function and execute the code directly, everything's working fine but the script is not secure. I want to call that function when i press submit button from this form:
<form method="post" action="">
      <input name="export" type="submit" value="Export" />
  </form>


function export_csv(){
// CSV CODE

    echo $out;
    exit;
 }

Open in new window

dsuciuAsked:
Who is Participating?
 
rjdownCommented:
On the page the form submits to (by the looks of it, the same page as the form itself), simply use:

<?php
if (isset($_POST['export'])) {
      export_csv();
}
?>
0
 
1209ylCommented:
In addition to what rjdown said, you should add to the form an action, like this:

<form method="post" action="$_SERVER['PHP_SELF']">
      <input name="export" type="submit" value="Export" />
  </form>

<?php
if (isset($_POST['export'])) {
      export_csv();
}
?>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.