Jump to content

Call java script function in php


cemzafer

Recommended Posts

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 codes

when  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 side

therefore 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 achieve

Paul
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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 code
into php and call the function.
Briefly, I have a php page and call .js code in the php code.
Thanks for your help.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.