Verification: a143cc29221c9be0

Php call function from url

Use jQuery to Execute the PHP Function With the onclick() Event

We can use jQuery to execute the onclick() event by writing a function that will execute the PHP function. For example, create a PHP file echo.php and write a function php_func(). Write a message Have a great day inside the function and call the function. In another PHP file, write some jQuery inside the script tag. Do not forget to link the webpage with the jQuery source. In the HTML, write a button tag with onclick() attribute. Write the value of the attribute as the test() function. Write the text Click between the button tags. Create an empty div tag below the button. Write the function test() inside the script tag. Write an AJAX method with the URL as echo.php and write a success() function with result as the parameter. Then use the selector to select the div tag and use the text() function with result as the parameter.

In the example below, we use the AJAX method to perform an asynchronous HTTP request. The URL species the URL to send the request to, and the success() function runs when the request is successful. The method sends the request to the echo.php file, which resides in the same location as the current PHP file. The request becomes successful, and the success() function returns the result, and it gets printed.

Example Code:

#php 7.x

Output:

Have a great day

Use Plain JavaScript to Execute the PHP Function With the onclick() Event

This method uses JavaScript to execute a PHP function with onclick() event. For example, write a PHP function php_func() that displays the message Stay Safe. Create a button with the name Click using the button tag. Specify the onclick() function as an attribute with the clickMe() function as its value. Write the function clickMe() inside the script tag. Create a variable result and call the php_func() in it inside the PHP tags. Use the document.write() function with the result as the parameter to print the output.

In the example below, the JavaScript function clickMe() executes when we click the button. Then, the PHP function php_func() executes from the JavaScript function. The result variable stores the result from the PHP function, and it gets printed.

Code Example:

#php 7.x

function clickMe(){
var result =""
document.write(result);
}

Output:

Stay Safe

Call PHP function from JavaScript inline

If you’re using PHP files to render your web pages, then you can call the PHP function from JavaScript by calling the PHP echo() function.

Suppose you have an index.php file with the following content:

function add($x, $y){
  return $x + $y;
}
?>

Then, you write your HTML content right below the function as follows:

function add($x, $y){
  return $x + $x;
}
?>

// Other HTML tags omitted for clarity
body>
  h1 id="header">h1>
body>

You can include a tag inside the tag that calls on PHP function as follows:

body>
  h1 id="header">h1>
  script>
    const result = php echo add(5, 3)?> ;
    document.getElementById("header").innerHTML = result;
  script>
body>

When you open your index.php file from the browser, you will see the HTML rendered as follows:

body>
  h1 id="header">8h1>
  script>
    const result = 8;
    document.getElementById("header").innerHTML = result;
  script>
body>

Any PHP code that you include inside your HTML page will be processed on the server-side before being served to the browser.

When you call a PHP function inline from JavaScript as shown above, you can’t use dynamic values from user input.

If you want to call PHP function as a response to user action, then you need to send HTTP request from JavaScript to the PHP file. Let’s learn that next.