How To Pass A Parameter To The Script

Table of contents:

How To Pass A Parameter To The Script
How To Pass A Parameter To The Script

Video: How To Pass A Parameter To The Script

Video: How To Pass A Parameter To The Script
Video: php Tutorials: How to pass parameters from one script to another 2024, November
Anonim

As applied to programming, the word "script" today is used to refer to a program written in one of the scripting programming languages. If the script needs to pass any parameter, then this is done, as a rule, when calling such a program. Other methods (for example, passing parameters through an intermediate file) also exist, but they are much less convenient to use.

How to pass a parameter to the script
How to pass a parameter to the script

Instructions

Step 1

If the script is called from an open browser window, then two methods of passing parameters are available - POST and GET. The POST method can be applied to scripts that are executed on the server and therefore have access to its "environment variables". These can be, for example, scripts in PHP or Perl languages. To pass a parameter by this method, place a form on the page with elements for entering parameters that will be sent to the script. Although, these can be not only text fields available to the user (text, textarea, password, checkbox, etc.), but also hidden fields of the hidden type. The action attribute of the form tag must contain the address of the script location, and the method attribute must specify the parameter transfer method (POST). For example, the html code for calling the script.php script passing it a parameter named someParam and the value 3.14 might look like this:

Step 2

Use an array of server superglobal variables to read the parameter passed to the script by the POST method. For example, the parameter passed from the form given in the previous step, the php-script will receive in the variable $ _POST ['someParam'].

Step 3

Another method of passing parameters (GET) can be used not only with server-side scripts, but also with executable on the client side - for example, written in JavaScript. With this method, the parameter is added directly to the script call line - it is added after the name of the script file through a question mark. For example, to call the script script.js with a parameter named someParam and the value 3.14, the script launch line might look like this: file: /// F: /sources/script.js? SomeParam = 3.14.

Step 4

Use the window.location.search property in the JavaScript script to read the string with the passed parameter, and in the php scripts, use the $ _GET superglobal array of server variables. In php scripts, this parameter can be used immediately (for example, in the form $ _GET ['someParam']), and JavaScript requires an additional user-defined function to retrieve the name and value of the passed variable.

Step 5

If you want to pass a parameter to ActionScript used in Flash games and other Flash-based elements, you can use the flashvars attribute of the embed tag. For example: Or using a similar construct for the object tag:

Step 6

Refer to the parameter passed in the way from the previous step as a _root variable. For example, for the sample from the previous step, the _root.someParam variable will contain the value 3.14.

Recommended: