Skip to content
Advertisement

Pass PHP variable from HTML form to Javascript

could you help me pass a variable that was taken from a html form into javascript.

The form asks for the tablename

JavaScript

It is passed through PHP:

JavaScript

A first action is underway, and created successfully: the Geoserver postgis table is created through PHP curl (entire code below)

then the javascript tries to get $table with a PHP insert; this will help it load the newly created postgis Geoserver table from the PHP, into the WFST javascript ;

JavaScript

JavaScript

But this code doesn’t work:

JavaScript

— The code works fine except for the thedata javascript variable. The entire form:

JavaScript

Into this javascript test1.php page:

JavaScript

Advertisement

Answer

In the code you provided, this:

JavaScript

…comes after this:

JavaScript

The initial assignment of PHP variable $table needs to come before it is used.

And if the table name, provided by the form text input, is a string, $table needs to be quoted for when it gets assigned to the Javascript variable thedata:


For example:

JavaScript

Keep in mind, PHP executes on the server, and the resulting text, containing HTML, Javascript, CSS, is sent to the browser through HTTP. The browser never gets PHP code. The browser only gets HTML, Javascript, and CSS. Once the browser gets these from the server it evaluates the Javascript and renders the HTML and CSS.

This, on the server:

JavaScript

…is executed resulting in this:

JavaScript

…which is then sent as text to the browser.

I elaborate further on the distinct PHP and Javascript execution contexts here: https://stackoverflow.com/a/72023066/2743458

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement