简体   繁体   English

如何在 html 按钮单击上调用 php 代码

[英]How to call a php code on html button click

i am working on a project that detects facial expressions in python.but i have to provide image to this code through php.The following php code saves image in directory.how can i call the following code in html button. i am working on a project that detects facial expressions in python.but i have to provide image to this code through php.The following php code saves image in directory.how can i call the following code in html button.

<?php
     function fun(){

    $img = $_POST['image'];
    $folderPath = "C:/xampp/htdocs/";

    $image_parts = explode(";base64,", $img);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];

    $image_base64 = base64_decode($image_parts[1]);
    $fileName = '1'. '.jpeg';

    $file = $folderPath . $fileName;
    file_put_contents($file, $image_base64);

    print_r($fileName);
    $command = escapeshellcmd("python C:/xampp/htdocs/generate_graph.py");
    $output = shell_exec($command);



 }
 ?>

HTML form should be like the blow if your form and PHP code are on the same page.如果您的表格和 PHP 代码在同一页面上,则 HTML 表格应该像打击一样。

<form action="" method="POST">
#Updading based on comment 
     <button type="submit" class="">Submit</button>
</form>

Remove the function fun(){}删除function fun(){}

Add the following:添加以下内容:

if(isset($_POST)){

  $img = $_POST['image'];
    $folderPath = "C:/xampp/htdocs/";

    $image_parts = explode(";base64,", $img);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];

    $image_base64 = base64_decode($image_parts[1]);
    $fileName = '1'. '.jpeg';

    $file = $folderPath . $fileName;
    file_put_contents($file, $image_base64);

    print_r($fileName);
    $command = escapeshellcmd("python C:/xampp/htdocs/generate_graph.py");
    $output = shell_exec($command);
}

Since your action is empty then it will hit the current page and if condition will work because of form submitting as POST method.由于您的操作为空,因此它将访问当前页面,并且由于表单作为POST方法提交, if条件将起作用。

Hope it will help.希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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