If your whole JavaScript code gets processed by PHP, then you can do it just like that. If you have individual .js files, and you don't want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript. For example, in your index.php (or wherever you specify your layout), you'd do something like

The "function" you have is server-side.Server-side code runs before and only before data is returned to your browser (typically, displayed as a page, but also could be an ajax request).. The form you have is client-side.This form is rendered by your browser and is not "connected" to your server, but can submit data to the server for processing. PHP: Your first PHP-enabled page - Manual The point of the example is to show the special PHP tag format. In this example we used . You may jump in and out of PHP mode in an HTML file like this anywhere you want. PHP: A simple tutorial - Manual Here we would like to show the very basics of PHP in a short, simple tutorial. This text only deals with dynamic web page creation with PHP, though PHP is not only capable of creating web pages. See the section titled What can PHP do for more information. PHP-enabled web pages are treated just like PHP - Complete Form - Tutorialspoint

How To Retrieve HTML Form Data With PHP

PHP - Functions - Tutorialspoint Creating PHP Function. Its very easy to create your own PHP function. Suppose you want to create …

PHP is so extensive that you can't to learn all facets in this tutorial. But PHP is not difficult! On the contrary, PHP is often very similar to plain English. Let's get started with your first PHP page. Example: Hello World! Start by making an ordinary HTML document, but name the file page.php and save it in the root of the site:

Lesson 3: Your first PHP pageentutorial - HTML.net