Today, We want to share with you call javascript function from php.In this post we will show you call javascript function from php and get return value, hear for call javascript function from html we will give you demo and example for implement.In this post, we will learn about How To Define And Call A Function In Javascript? with an example.
how to call javascript function from php with parameters?
A JavaScript function is a group of reusable all the source code which can be called any where in your web page or script. This terminate the required of writing the same source code repete and repete. It useful programmers in writing modular or MVC based source codes. Functions allow a devloper to divide a big script project into a number of small as well as manageable functions.
Calling a JavaScript function from PHP
<?php ?> <script> function getDataFunc(text){ alert(text); } </script> <script> var JSDATA = <?=json_encode($jsdata, JSON_HEX_TAG | JSON_HEX_AMP )?>; getDataFunc(JSDATA) </script> <?php ?>
Example 2 : Single Parameter
<?php echo "https://www.pakainfo.com/javascript-function/'>Full example at: https://www.pakainfo.com/javascript-function/</a>"; echo "<p>Add whatever PHP you want here...</p>"; $jsdata = 'virat'; ?> <script> function getDataFunc(text){ alert(text); } </script> <script> var JSDATA = <?=json_encode($jsdata, JSON_HEX_TAG | JSON_HEX_AMP )?>; getDataFunc(JSDATA); </script> <?php ?>
Example 3 : Array Parameters
<?php echo "<a href='https://www.pakainfo.com/javascript-function/'>Full example at: https://www.pakainfo.com/javascript-function/</a>"; echo "<p>Add whatever PHP you want here...</p>"; $myname = 'virat'; $yourname = 'YourName'; $jsdata = array( 'input' => $myname, 'array_input' => array( 'name' => $yourname ), ); ?> <script> function getDataFunc(text){ alert(text); } </script> <script> var JSDATA = <?=json_encode($jsdata, JSON_HEX_TAG | JSON_HEX_AMP )?>; getDataFunc(JSDATA.input); getDataFunc(JSDATA.array_input.name); </script> <?php ?>
How to trigger or call the javascript function from PHP?
<script> function display() { document.write("Hello Pakainfo."); } </script> <?php echo '<script>display();</script>'; ?>
Output:-
Hello Pakainfo.
create a javascript function call by a button event
<script type="text/JavaScript"> function show_hide() { var x = document.getElementById("nums_logo"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } </script> <?php echo "<button onClick='show_hide()'>Show/Hide</button>"; echo'<p id="nums_logo" style="display:none;">Welcome to Pakainfo.com.</p>'; ?>
What is a Javascript function?
The simple defination of the Javascript is the client-side basic script that can add, delete as well as modify the data contents & designs of an HTML part.
Example
<html> <body> <script> function add(n1, n2) { return n1 + n2; } document.write(add(12, 10)); </script> </body> </html>
call javascript function from php
<?php echo '<script type="text/javascript">', 'someJsFunc();', // Or Whatever '</script>'; ?>
how to call js function from php
<?php if(your js condition){ echo "<script> window.onload = function() { yourJavascriptFunction(param1, param2); }; </script>"; ?>
php execute javascript
<?php if(your js condition){ echo "<script> window.onload = function() { yourJavascriptFunction(param1, param2); }; </script>"; ?>
I hope you get an idea about call javascript function from php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.