Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

how to call a PHP function from a form button

I have a form in my index.html that looks like this:

<form name="form" method="post" action="send.php">
  .
  .
  .
  .

Inside my send.php I have to functions, function generatekey () and function postData(), how call I call the postData() function from my action attribute in my form?

Upvotes: 1

Views: 189

Answers (3)

Tomer
Tomer

Reputation: 17940

Basically there is no "easy" way to do that, you need to write some code in your script that will decide what function to call and when, like:

if($_POST['submit'){
   postData();
}

Another option is to use one of the many MVC framework out there like Codeigniter or laravel.

Upvotes: 0

Adi
Adi

Reputation: 5179

you can also make your action like this:

<form name="form" method="post" action="send.php?postData">

and in your send.php you can do this:

if(isset($_GET['postData'])){
   postData();
}

Upvotes: 2

Peon
Peon

Reputation: 8030

Add a unique hidden field in your form like:

<input type="hidden" name="action" value="postData" />

send.php

<?php

    function generatekey () {
      // action
    }

    function postData() {
      // action
    }

    if ( $_POST[ 'action' ] == 'postData' ) {
        postData();
    }

?>

Or read your submit value, if it's unique.

Upvotes: 1

Related Questions