rm_phpfreaks Posted April 4, 2009 Share Posted April 4, 2009 Hi, Some website said php is on server side and javascript is on client side we cannot run php function in javascripts events... here's how...hope you can help me to improve my code! cheers!! function myfunction(){ $x="I love PHP by Arman de Guzman de Castro :-)!"; return $x; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script language="javascript" type="text/javascript"> function ILovePHP() { b = "<?=myfunction();?>"; alert(b); } </script> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <form action="" method="post"><input name="" name="" type="text" onChange="ILovePHP();"></form><p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/152498-calling-php-function-on-javascript-events-eg-onchange/ Share on other sites More sharing options...
MadTechie Posted April 4, 2009 Share Posted April 4, 2009 PHP = server side JS = client side. by the time the JS is ready to run a function php would of finished running.. SO you can't.. you could try ajax that allows you to get results from PHP, so thats probably what your looking for! Quote Link to comment https://forums.phpfreaks.com/topic/152498-calling-php-function-on-javascript-events-eg-onchange/#findComment-800963 Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 Yes, you need AJAX. And please remember to use the tags to properly format and color your code. Quote Link to comment https://forums.phpfreaks.com/topic/152498-calling-php-function-on-javascript-events-eg-onchange/#findComment-800970 Share on other sites More sharing options...
MadTechie Posted April 4, 2009 Share Posted April 4, 2009 And thanks for shopping at PHPFreaks.com I mean Welcome to PHP Freaks if you need more info about Ajax see the ajax section (note theirs some working examples stickied at the top) Quote Link to comment https://forums.phpfreaks.com/topic/152498-calling-php-function-on-javascript-events-eg-onchange/#findComment-800972 Share on other sites More sharing options...
peuge Posted April 4, 2009 Share Posted April 4, 2009 I find the quickest way is to create an empty iframe, no one will be seeing this, set it to 1pxx1px. Make a php file with only your function in it. Then make a javasctipt function: function fName(var){ var objIframe = document.getElementById('Your iframe id'); var url = 'function.php?variable='+var; objIfram.src = url; } Then in your function.php get your variables. $variable = $_GET['variable']; Then just call your javascript function. Of course you can send it many variable and just add those into the function declaration and then also add then into your url: url= 'function.php?var1='+var1+'&var2='+var etc etc This is only for processing things like database inserts and stuff which does not require display. also your html must be: <input name="" name="" type="text" onChange="fName(this.value);"> If you do want to display stuff then you can expand the iFrame, but then you using iFrames Quote Link to comment https://forums.phpfreaks.com/topic/152498-calling-php-function-on-javascript-events-eg-onchange/#findComment-801074 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.