+ 5

Javascript function in PHP

How can I call a javascript function in PHP and execute the function on data that is being retrieved from a database? Any help will be greatly appreciated

18th Jun 2018, 2:15 PM
Kabulput
Kabulput - avatar
8 Answers
+ 8
JavaScript and PHP are both scripting languages but not much close that they can handle each other function though there are many ways to do simple task with PHP and js like: $sql = "INSERT INTO test_db (username,password) VALUES ('name','pass')"; if (mysqli_query($sql)) { echo "<script>"; echo "alert('Query succes')"; echo "</script>"; }else{ echo "<script>"; echo "alert('Query failed')"; echo "</script>"; } like this method i think we can call js function inside php echo "<script>"; echo "test()"; echo "</script>"; USING AJAX can be helpful too
18th Jun 2018, 2:42 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 6
Wendy Seijas Thanks so much
18th Jun 2018, 7:54 PM
Kabulput
Kabulput - avatar
+ 5
PHP would not call a JavaScript function. They are executed in different places. PHP run on server, whereas JavaScript run on client pc.
18th Jun 2018, 2:21 PM
CalviŐ˛
CalviŐ˛ - avatar
+ 5
using JSON is the best solution.. i guess
18th Jun 2018, 2:42 PM
Andrew Ting Mai Zau
Andrew Ting Mai Zau - avatar
18th Jun 2018, 2:50 PM
Kabulput
Kabulput - avatar
+ 4
Shudarshan Rai 👑 Thanks I will try that method
18th Jun 2018, 2:49 PM
Kabulput
Kabulput - avatar
+ 4
for your solution, the following link is a good one...try it.. http://blog.oskoui-oskoui.com/?p=1262
18th Jun 2018, 2:52 PM
Andrew Ting Mai Zau
Andrew Ting Mai Zau - avatar
+ 3
PHP only parsing JavaScript code, it never can execute it.
18th Jun 2018, 2:46 PM
CalviŐ˛
CalviŐ˛ - avatar