cemzafer Posted December 17, 2006 Share Posted December 17, 2006 Hi all,I want to call a java script funtion in php, so my java script functionname is dateform. So how can i call this function in php?Thanks for your advice. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 17, 2006 Share Posted December 17, 2006 PHP can not call a Javascript function, since PHP runs on the server while Javascript runs on the client.Please post you script so we know what you are trying to accomplish.Ken Quote Link to comment Share on other sites More sharing options...
paul2463 Posted December 17, 2006 Share Posted December 17, 2006 what Ken has said is exactly as it works, I had it explained to me in words of not many sylables, and I thought I would let you into this simple way of thinking about to understand what you need to do... on your page containing PHP and Javascript and HTML codeswhen you request to look at that page, all the PHP code is parsed and carried out on the server before the information is passed to you, then all the HTML and Javascript code is parsed and run on your client sidetherefore if you have a a javascript function called createError() and call it in the php code, the php with throw an error such as "Call to undefined function" because the javascript code does not exist at this point in time. hope that helps in the explanation......but as with all things there are workarounds and again as Ken says tell us what you want to achievePaul Quote Link to comment Share on other sites More sharing options...
mainewoods Posted December 17, 2006 Share Posted December 17, 2006 You can't call javascript from php but what you can do is conditionally write out javascript code on the server side just like you conditionally write out html on the server side.What are some uses of this? I can use php to insert a static literal into a javascript function call on the server side:[code]<?php echo "<a href=\"javascript: dopageupdate('$serversidevar');\">link</a>";?>[/code]--the php $serversidevar variable will be inserted into the javascript function as a literal value server side and then will be returned to the browser as a javascript link with a literal value in it:[code]<a href="javascript: dopageupdate('94dodgevan');">Show 94 Dodge Van Details</a>[/code]--that code could be used initiate a call through ajax to get the detail info about the product.--I am dynamically generating a dhtml interface from php. I make a change to a php data var and it cascades through to the client side dhtml as well. Why would I want to do this? Because many times part of the processes have to be initialized server side and then have to be reflected in the dhtml as well. Another use of conditionally writing javascript from php is the case of a form submitted and then the form failing data validation server side. You would want the form to redisplay and maybe cause a pop up javascript box to inform them of problems with the form. You could do something like this server side:[code]<body <?php if ($formerrorsfound) echo ' onload="alert(\'You have entry errors\');"'; ?>>[/code]--if there were no data entry errors the php would return:[code]<body>[/code]--whereas if there are errors it would return:[code]< body onload="alert('You have entry errors');">[/code]php and javascript do work together, they work together in the very very same way that php and html work together. html is output by php server side as just strings, the html doesn't get parsed and rendered until it reaches the browser. Likewise javascript can be output variably as strings by php server side which then is returned to the browser to be parsed and executed. [color=red][b]I can use php to change the javascript behavior of the web page returned to the browser! 8)[/b][/color] Quote Link to comment Share on other sites More sharing options...
cemzafer Posted December 19, 2006 Author Share Posted December 19, 2006 So here is my definition,I have a calendar.js java script, this .js popup a calendar to the page.anyway I want to call this java script code with using php or i want to embed the js codeinto php and call the function.Briefly, I have a php page and call .js code in the php code.Thanks for your help. Quote Link to comment 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.