Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在html按钮上调用php代码单击_Javascript_Php_Python_Html - Fatal编程技术网

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

Javascript 如何在html按钮上调用php代码单击,javascript,php,python,html,Javascript,Php,Python,Html,我正在从事一个用python检测面部表情的项目。但我必须通过php向该代码提供图像。以下php代码将图像保存在目录中。我如何在html按钮中调用以下代码 <?php function fun(){ $img = $_POST['image']; $folderPath = "C:/xampp/htdocs/"; $image_parts = explode(";base64,", $img); $image_type_aux = explode

我正在从事一个用python检测面部表情的项目。但我必须通过php向该代码提供图像。以下php代码将图像保存在目录中。我如何在html按钮中调用以下代码

<?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);



 }
 ?>

如果您的表单和PHP代码在同一页面上,HTML表单应该像打击一样

<form action="" method="POST">
#Updading based on comment 
     <button type="submit" class="">Submit</button>
</form>
由于您的操作是空的,因此它将命中当前页面,并且由于表单以
POST
方法提交,因此
if
条件将起作用


希望这会有帮助

从HTML页面连接到服务器有两种主要方法:表单元素和AJAX调用(=XMLHttpRequest)。您应该能够通过tagi add button in form tag将PHP脚本URL超链接到该按钮上。每当我单击按钮时,它都不会做任何事情。我对PHP了解不多。我认为我将此代码放在了错误的位置。我仍然没有收到按钮单击的响应。我将给定的PHP代码放在标记之后。它是正确的位置吗?可以吗请在您的问题中转储整个代码?它正在工作。我还有一个问题。我必须每10秒运行一次此代码。我如何才能做到这一点?您必须使用
javascript
作为最后一个问题。
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);
}