Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call PHP functions by JavaScript?

I am trying to call a PHP function from an external PHP file into a JavaScript script. My code is different and large, so I am writing a sample code here.

This is my PHP code:

<?php function add($a,$b){   $c=$a+$b;   return $c; } function mult($a,$b){   $c=$a*$b;   return $c; }  function divide($a,$b){   $c=$a/$b;   return $c; } ?> 

This is my JavaScript code:

<script>   var phpadd= add(1,2); //call the php add function   var phpmult= mult(1,2); //call the php mult function   var phpdivide= divide(1,2); //call the php divide function </script> 

So this is what I want to do.

My original PHP file doesn't include these mathematical functions but the idea is same.

If some how it doesn't have a proper solution, then may you please suggest an alternative, but it should call values from external PHP.

like image 263
wallgeek Avatar asked Apr 02 '13 06:04

wallgeek


People also ask

How do you call a PHP function?

In PHP, a function is declared with the function keyword prefixed with the function name and the calling of a function in a program is done by just calling the name of the function wherever required.

How do you call a PHP function in HTML?

Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.


1 Answers

Yes, you can do ajax request to server with your data in request parameters, like this (very simple):

Note that the following code uses jQuery

jQuery.ajax({     type: "POST",     url: 'your_functions_address.php',     dataType: 'json',     data: {functionname: 'add', arguments: [1, 2]},      success: function (obj, textstatus) {                   if( !('error' in obj) ) {                       yourVariable = obj.result;                   }                   else {                       console.log(obj.error);                   }             } }); 

and your_functions_address.php like this:

    <?php     header('Content-Type: application/json');      $aResult = array();      if( !isset($_POST['functionname']) ) { $aResult['error'] = 'No function name!'; }      if( !isset($_POST['arguments']) ) { $aResult['error'] = 'No function arguments!'; }      if( !isset($aResult['error']) ) {          switch($_POST['functionname']) {             case 'add':                if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 2) ) {                    $aResult['error'] = 'Error in arguments!';                }                else {                    $aResult['result'] = add(floatval($_POST['arguments'][0]), floatval($_POST['arguments'][1]));                }                break;              default:                $aResult['error'] = 'Not found function '.$_POST['functionname'].'!';                break;         }      }      echo json_encode($aResult);  ?> 
like image 75
Victor Avatar answered Oct 10 '22 10:10

Victor