Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

javascript - 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.

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



 }
 ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

HTML form should be like the blow if your form and PHP code are on the same page.

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

Remove the 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.

Hope it will help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...