简体   繁体   中英

Call PHP function on click of a html button

I would like to call a php function after clicking a button. I already found a way to do this (kind of).

This is my code:

info.html

<html>
 <head>    
 </head>
 <body>  

<input type=button value="test" onClick="self.location='http://127.0.0.1/info.php?runFunction=main'">

 </body>
</html>

info.php

<?php    

    if(isset($_GET['runFunction']) && function_exists($_GET['runFunction']))
    call_user_func($_GET['runFunction']);
    else
    echo "Function not found or wrong input";

    function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle ,1024,";");
    }
    fclose($file_handle);
    return $line_of_text;
    }

    function main($csvFile){

        //Set path to CSV File

        $csv = readCSV($csvFile);
        echo '<pre>';
        print_r($csv);
        echo '</pre>';

    }  


?>

My button is able to call the main function, but I do not know how to pass on a variable with a button click, could anybody help me with this?

You could pass the argument as another URL parameter:

<input type=button value="test" onClick="self.location='http://127.0.0.1/info.php?runFunction=main&arguments[]=File.csv'">

Then the PHP would be:

if(isset($_GET['runFunction']) && function_exists($_GET['runFunction'])) {
    if (isset($_GET['arguments'])) {
        $args = $_GET['arguments'];
    } else {
        $args = array();
    }
    call_user_func_array($_GET['runFunction'], args);
} else {
    echo "Function not found or wrong input";
}

Putting [] after the parameter name in the URL tells PHP to collect all the parameters with this same name into an array.

However, this is extremely dangerous, since it allows someone to execute any PHP function. Someone could connect to a URL like info.php?runFunction=unlink&arguments[]=.htaccess .

You should check the function name against a list of allowed functions to call.

You have to make a AJAX call . You can pass any arguments in that via GET or POST method. AJAX is simplest way to do this.

You should use Ajax to send data to the server

<script>

function sendData(){
  var data1 = "Hello";
  var data2 = "World";
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function(){
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        alert(xmlhttp.responseText);
  }
  xmlhttp.open("GET", "ajax.php?data1=" + data1 + "&data2=" + data2, true);
  xmlhttp.send();
}

</script>

You call the sendData function, when the button is clicked:

<input type=button value="test" onClick="sendData()">

The server reads the GET-Parameter

if(isset($_GET['data1']) && isset($_GET["data2"])){
  $data1 = $_GET["data1"];
  $data2 = $_GET["data2"];

  return $data1 . " " . $data2 . " was sent to the server";
}

Change

<input type=button value="test" onClick="self.location='http://127.0.0.1/info.php?runFunction=main'"

to

<a href="info.php?runFunction=main"><input type=button value="test"></a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM