0

i have a function in php .i want to call it inside javascript function .How to do that? My php function is

<?php
    function image_save(){
        $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);
    }

?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • 2
    In essence you cannot call a php function in a javascript function. You can call the php function if you use ajax to make the request and use a callback to process your PHP function's response – Professor Abronsius Nov 21 '19 at 13:08
  • 1
    Either by submitting a form, or with ajax – Thomas Nov 21 '19 at 13:08
  • https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Teemu Nov 21 '19 at 13:17

2 Answers2

1

You can call an API from js running on your broswser, not directly a PHP function.

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
1

If you want to call a PHP function from Javascript, you need to send an XMLHttpRequest to a server on which this function is running. Javascript is running in the browser, at your client computer. And PHP is running on your server, in some datacenter somewhere. Therefore, it's impossible to call a PHP function from your Javascript directly.

The XmlHttpRequest send an http request ( POST | GET | ... ) to a server and wait for it's output. In your case, you would call a php file on a server and wait for it's response.

From Javascript.info

// 1. Create a new XMLHttpRequest object
let xhr = new XMLHttpRequest();

// 2. Configure it: GET-request for the URL
xhr.open('GET', '/yourPhPfileLocationUrl/');

// 3. Send the request over the network
xhr.send();

// 4. This will be called after the response is received
xhr.onload = function() {
  if (xhr.status != 200) { // analyze HTTP status of the response
    alert(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found
  } else { // show the result
    alert(`Done, got ${xhr.response.length} bytes`); // responseText is the server
  }
};

xhr.onprogress = function(event) {
  if (event.lengthComputable) {
    alert(`Received ${event.loaded} of ${event.total} bytes`);
  } else {
    alert(`Received ${event.loaded} bytes`); // no Content-Length
  }

};

xhr.onerror = function() {
  alert("Request failed");
};

There is also numerous libraries out there to do way simpler XMLHttpRequest.

You could also submit a form, it might be easier.

<form action="/yourPHPFunctionUrl">
<!-- some inputs -->
<button type="submit">Send me ! </button>
</form>
Nicolas
  • 8,077
  • 4
  • 21
  • 51