HTML Button触发PHP代码

时间:2018-08-28 04:04:39

标签: php html

我有一个demo.php,其中有一个按钮:

<?php

include('../library/Requests.php');

Requests::register_autoloader();

function get_data(){
    $request = Requests::post('http://localhost:8000/api/groups/list/', array(), null);

    var_dump($request);
}

?>

<button>Click Me</button>

如何在get_data中触发demo.php方法?谁能告诉我解决方案?

1 个答案:

答案 0 :(得分:2)

您想让自己看起来简单...

<?php

include('../library/Requests.php');

Requests::register_autoloader();
// check whether form is submitted or not on same page if submitted then call get_data() function
if(isset($_POST['submitBtn']))
{
     get_data();
}
function get_data(){
    $request = Requests::post('http://localhost:8000/api/groups/list/', array(), null);

    var_dump($request);
}


?>

// make form that could send request
<form action="" method="post">
     <button name="submitBtn" type="submit">Click Me</button>
</form>