How To Pass A Value

Table of contents:

How To Pass A Value
How To Pass A Value

Video: How To Pass A Value

Video: How To Pass A Value
Video: Java Tutorial - Passing by Value (Object References) 2024, May
Anonim

The most attractive for website visitors, and therefore the most important for their creators, are their interactive capabilities. That is, the ability for a visitor to send some information (or a request for information) to the server and receive a response from him. The organization of this process requires the transfer of variables from pages in the user's browser to server scripts. The most common today are: from the page description languages - HTML (HyperText Markup Language - "hypertext markup language"), and from the server-side scripting languages - PHP (Hypertext Preprocessor - "hypertext preprocessor"). We will consider the simplest options for passing variables from HTML pages to PHP scripts.

Sending variables to php script
Sending variables to php script

It is necessary

Basic knowledge of PHP and HTML languages

Instructions

Step 1

The first part of the task (passing variables from HTML pages) is solved by placing in the page code those form elements that are more suitable for user input and transfer of the required data type. These can be "text", "textarea", or "password" text fields, "checkbox" or "radio" radio buttons, "select" lists, a "file" file selection field, "input" buttons, or "hidden" fields. The HTML code of each of these elements must contain the "name" tag - it contains the name of the variable being passed. For example, the HTML for a multi-line textarea might look like this:

here is the default text

And the code for the hidden field is like this:

Any of the form elements must be placed inside the form tags. The opening tag looks like this:

Here the "action" tag specifies the name of the server script to which the sent variables should be passed, and the "method" tag specifies the method of data transfer. There can be only two ways - POST or GET. The main difference between them is that with the GET method, variables are passed along with the page address (URL), and with the POST method, in a special area of network packets (header).

The form's closing tag is simple:

And, of course, a button should be added to the form so that the user can give a command to send variables to the server. As a result, the HTML code of the form with elements for sending variables to the server script may look like this:

here is the default text

HTML form
HTML form

Step 2

Now let's look at how to access the variables sent to it from the server php script. Everything is very simple here - variables sent by the GET method are placed into the $ _GET superglobal array, and those sent by the POST method - into the similar $ _POST array. There is another superglobal array, $ _REQUEST. All variables fall into it, regardless of how they were passed. The simplest php code that prints information about the variables received from the form with the names hideMe and strings might look like this:

<? php

if ($ _ POST) {

echo ('The variable hideMe contains the value "'. $ _ POST ['hideMe']. '"

');

echo ('The variable strings contains the value "'. $ _ POST ['strings']);

}

?>

Here the "if" comparison operator is used to check if any variables were POST at all. If you combine the code of the HTML form and the PHP script in one PHP file, then after clicking the "Submit" button, we get the following result:

PHP script
PHP script

Step 3

We see that we have solved the problem of sending parameters from an HTML form and receiving them with a PHP script.

Recommended: