Javascript Function call php Function?
Moderator: General Moderators
- Pyrite
- Forum Regular
- Posts: 769
- Joined: Tue Sep 23, 2003 11:07 pm
- Location: The Republic of Texas
- Contact:
Javascript Function call php Function?
Is it possible for me to have a JS function that calls a php function inside it. Like I have a JS function that is called with a string, and then that JS could call the php setcookie function with that string?
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Ummm not really as javascript is client side and not server side. The way you can get it to work is to have the javascript reload the page with a GET value which causes a function to be run when php parses the page.
Code: Select all
if($_GET[setcookie]){
//do cookie stuff.
}You can get javascript to call PHP functions, and even get data back from PHP functions, without loading the page (or even using iframes).
Add a script tag to your document, give it a URL that loads a blank document. Give that script tag an ID. Now, using javascript, you can modify the src of the script tag, including query string. Because of that, you can pass arguments to a PHP page, which can spit out javascript containing the data. I used this method for a database-fuelled drop-down list (which returns entries from the database that match what you have typed, akin to the windows autocomplete feature, but web-based and fed by DB).
Add a script tag to your document, give it a URL that loads a blank document. Give that script tag an ID. Now, using javascript, you can modify the src of the script tag, including query string. Because of that, you can pass arguments to a PHP page, which can spit out javascript containing the data. I used this method for a database-fuelled drop-down list (which returns entries from the database that match what you have typed, akin to the windows autocomplete feature, but web-based and fed by DB).