how to call a PHP function from a form button

前端 未结 3 485
别那么骄傲
别那么骄傲 2021-01-24 13:29

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

. . . .
<
相关标签:
3条回答
  • 2021-01-24 13:40

    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.

    0 讨论(0)
  • 2021-01-24 13:43

    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();
    }
    
    0 讨论(0)
  • 2021-01-24 14:04

    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.

    0 讨论(0)
提交回复
热议问题