Including a JS file into a PHP file and passing values back

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Including a JS file into a PHP file and passing values back

Post by impulse() »

Hi.

I have a PHP page which uses Ajax to fetch MySQL database results. The Ajax function is included in the head as a .js file and the PHP code to query the database is included in the head, also as a Javascript file, but the extension is .PHP

Code: Select all

<script language="javascript" type="text/javascript" src="./js/search_suggestions.php"></script>
But I want to be able to pass a GET variable from the PHP that is loaded in the browser back to the search_suggestions.php page so the database querying can be more dynamic.

How could this be done? I've played about but the search_suggestions.php page doesn't read the GET variables from the page loaded by the browser.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Typically, you include a PHP script that uses JSON to initialize/communicate to the Javascript, then the Javascript sets up communication back to PHP with Ajax. You can also have PHP push JSON back to the browser. There are a number of libraries that can help you with this.
(#10850)
Post Reply