-1

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?

Fynn
  • 210
  • 1
  • 6
  • 28

4 Answers4

1

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

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.

  • Could you provide me an example? I have never used AJAX before and am very new to PHP – Fynn Feb 20 '15 at 12:44
  • hey I can not provide you example right now. But you can go through some docs here: http://www.w3schools.com/jquery/jquery_ajax_intro.asp –  Feb 20 '15 at 12:46
  • 1
    This has already been stated in comments. Provide the OP with an example, based on their code. Who upvoted this anyway? – Funk Forty Niner Feb 20 '15 at 12:52
0

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";
}
maxeh
  • 1,373
  • 1
  • 15
  • 24
  • Do i have to import some sort of library, like jquery or do anything else? I do not have a file called ajax.php, do i have to create one? Please explain your answer a bit further, because i am new to PHP – Fynn Feb 20 '15 at 12:54
  • ajax.php is just an example name, of course you can chose your own name (you have to create a .php file) You dont have to import librarys or jquery. You need: 1 PHP File (your website) , 1 PHP File for the Ajax Request (you send your data to this .php file) and your JavaScript File (or you use – maxeh Feb 20 '15 at 13:02
  • Could it be that I need to use &_GET("data1"); instead of &_GET["data1"]? Somehow that code is not working for me Whenever i try to use ["data1"] i get the error: Syntax error, unexpected "[", expecting "(" in test.php on line 3 But when I use ("data1") i get: Fatal error: Call to undefined function _GET() in test.php on line 3 – Fynn Feb 23 '15 at 07:17
  • When did you started to work with PHP? You should read some tutorials first, this is pretty basic stuff. What is this "&" ? In PHP you use "$". You can't write complex scripts when you don't understand what you do. That's quite important. EDIT: I just saw that i wrote that in my example, too, i clicked the wrong button, however I said it is not tested – maxeh Feb 23 '15 at 08:42
0

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>
erashdan
  • 152
  • 1
  • 9
  • This does not solve my problem on how to pass on a variable, it is just a different way to do what already works – Fynn Feb 20 '15 at 13:11