radiations3 Posted April 3, 2012 Share Posted April 3, 2012 How can i call javascript function from php code?? (Kindly Help) As an example (idea) <script language="javascript"> function divclose() { alert("I am an alert box!"); } </script> <? echo "<SCRIPT LANGUAGE='javascript'>divclose();</SCRIPT>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/ Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 That code should work (besides short open tags maybe not being enabled), what is the issue? Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333882 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 the alertbox doesn't appear if i simply put in html tags and call function from there it works but not when call them from php :-/ Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333884 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 echo'ing your data using php or hard coding it makes no difference to the browser. What does "view source" show as your markup? Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333885 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 the view source shows the coding without 'any php code inside it'.... Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333888 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 Show us. Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333891 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 What actually i am trying to do is to check whether any body logged in or not and if someone is logged in then in it shows the divs with id = 2 else it shows the divs with id = 4 i am using showing and hiding of divs coding via javascripting but the functions are not being called from php dont know why and how kindly help... Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333893 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 Show us. <script language="javascript"> function jsfunction () { $("2").show(); $("1").hide(); alert("I am an alert box!"); } function jsfunc() { $("2").hide(); $("1").show(); alert("I am an alert box!"); } hijsfunction();</script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <p> </p> <div id="2" > hello </div> <div id="1" > Fellow </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333895 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 No where in that code do you call any of the functions declared. Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333897 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 My complete code the old code was copied from view source section of chrome (this code is also checked by calling javascripting function via echo "<SCRIPT LANGUAGE='javascript'>jsfunction();</SCRIPT>" <script language="javascript"> function jsfunction () { $("2").show(); $("1").hide(); alert("I am an alert box!"); } function jsfunc() { $("2").hide(); $("1").show(); alert("I am an alert box!"); } </script> <?php //initialize the session if (!isset($_SESSION)) { session_start(); } if (isset($_SESSION['MM_Username'])) { echo "hi"; print("jsfunction();"); } else { print("jsfunc();"); echo "cool"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <p> </p> <div id="2" > hello </div> <div id="1" > Fellow </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333900 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 That is not the html source, or if it is, it has php in it. Looking at that code I can see that your not calling the javascript functions within <script></script> tags. You do understand that PHP is processed server side long before your markup is displayed (and javascript executed) client side. Why don't you just wrap your divs in an if statement and not produce them if you don't need them? Your approach really makes little sense. Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333902 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 i tried that too but nothing happend Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333904 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 What do you meen nothing happened? Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333905 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 This code worked <script type='text/javascript'> function mySuperAwesomeFunction(){ alert('Yay, this works!'); } <?php print("mySuperAwesomeFunction();"); ?> </script> but when i use same code in mine nothing happend all i want to do is the following <script language="javascript"> function jsfunction () { $("2").show(); $("1").hide(); alert("I am an alert box!"); } function jsfunc() { $("2").hide(); $("1").show(); alert("I am an alert box!"); } <?php //initialize the session if (!isset($_SESSION)) { session_start(); } if (isset($_SESSION['MM_Username'])) { echo "hi"; //call javascripting function here } else { print("jsfunc();"); //call javascripting function here echo "cool"; } ?> </script> Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333907 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 You really need to read my previous reply again. Your entire logic is floored. Why are you using JavaScript to hide the divs when you can simply not send them to the client using php. I meen really, this logic is terrible. Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333908 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 ok kindly can you give me a short example so i can workout regarding the approach you told me to work on..?? Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333910 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 if ($someexpression) { echo '<div>some div</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333911 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 hmmmmm well your approach really worked but kindly let me know of the code if i can use my php code to call any function from javascript like if ($x=1) echo "<SCRIPT LANGUAGE='javascript'>divclose();</SCRIPT>"; i just want to know that's all cause i have wasted my time alot for this ..... Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333915 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 You have already posted an example that you said did work. Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333917 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 You have already posted an example that you said did work. true but it doesnot when i put it in my code like (every single bit is same as the code i said was working but dont know why this one doesnot work) <script type='text/javascript'> function mySuperAwesomeFunction(){ alert('Yay, this works!'); } <?php //initialize the session if (!isset($_SESSION)) { session_start(); } if (isset($_SESSION['MM_Username'])) { echo "hi"; print('mySuperAwesomeFunction();'); } else { echo "<SCRIPT LANGUAGE='text/javascript'>jsfunc();</SCRIPT>"; echo "cool"; } ?> </script> Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333919 Share on other sites More sharing options...
trq Posted April 3, 2012 Share Posted April 3, 2012 So, a few posts ago I asked you to show us the source. You still haven't. If you ask for help you should read the replies. Also, not that you cannot call session_start after sending out. Turn error_reporting on and you will see your php code produces errors. Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333920 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 So, a few posts ago I asked you to show us the source. You still haven't. If you ask for help you should read the replies. Also, not that you cannot call session_start after sending out. Turn error_reporting on and you will see your php code produces errors. Well sir i read all of your replies and respond accordingly i may missunderstood something so sorry for that you have really helped me alot and gave me an alternative i have tried error_reporting(); but it didnot report any i'll try to google to get the solution of how to do javascripting in echo... Thanx alot!! Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333963 Share on other sites More sharing options...
batwimp Posted April 3, 2012 Share Posted April 3, 2012 There are a couple of problems with the last bit of code you posted. First, you really should close your <script> tags after the actual javascript and not the entire page. Second, when calling the javascript function from a PHP print command, use the full javascript syntax: echo "<SCRIPT type='text/javascript'>jsfunc();</SCRIPT>"; Third, notice that I changed your code from SCRIPT LANGUAGE='text/javascript' to SCRIPT type='text/javascript'. Fourth, you weren't calling the same function as the one you defined. You were still using mysuperawesomefunction but the code in the script tags were calling jsfunc(). Here is your code with the modifications made, and it works on my system: <script type='text/javascript'> function jsfunc(){ alert('Yay, this works!'); } </script> <?php //initialize the session if (!isset($_SESSION)) { session_start(); } if (isset($_SESSION['MM_Username'])) { echo "hi"; echo "<SCRIPT type='text/javascript'>jsfunc();</SCRIPT>"; } else { echo "<SCRIPT type='text/javascript'>jsfunc();</SCRIPT>"; echo "cool"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1333974 Share on other sites More sharing options...
radiations3 Posted April 3, 2012 Author Share Posted April 3, 2012 ok i get it know one last question kindly let me know what is wrong with the following code : (why isn't it hiding the div with id=2) <script type='text/javascript'> function toggle(id) { document.ElementById("id").style.visibility = "hidden"; } </script> <body> <SCRIPT type='text/javascript'>toggle(2);</SCRIPT> <p> </p> <div id="2" > hello </div> <div id="abc" > Fellow </div> </body> Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1334017 Share on other sites More sharing options...
batwimp Posted April 3, 2012 Share Posted April 3, 2012 The most important problem you have there is that the javascript function is called getElementById, not ElementyById, though this doesn't solve everything. In troubleshooting javascript, you need to mess around with things like code placement (where in the script your code is), single quotes vs. double quotes vs. no quotes at all, etc. After playing around with this stuff, I got this code to work. Note the placement of the script tags and quotes: <script type='text/javascript'> function toggle(id) { document.getElementById(id).style.visibility = "hidden"; } </script> <body> <p> </p> <div id="2" > hello </div> <div id="abc" > Fellow </div> <SCRIPT type='text/javascript'>toggle(2);</SCRIPT> </body> Quote Link to comment https://forums.phpfreaks.com/topic/260252-how-can-i-call-javascript-function-from-php-code-kindly-help/#findComment-1334025 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.