Simple page switcher in php. PHP and forms

Often on Web sites you can find pages with HTML forms placed on them. Web forms are a convenient way to receive information from visitors to your site. An example of this is the guest book, which provides feedback to visitors and site developers. Forms are also convenient for site developers when developing a CMS, which allows them to maintain the main property of the site - relevance. This article covers the basics of creating HTML forms, processing them, and ways to transfer data from screen forms to PHP scripts.

1) Create a simple form

Tags

And
define the beginning and end of the form. Starting form tag
contains two attributes: action And method. The action attribute contains the URL of the script that must be called to process the script. Attribute method tells the browser what type of HTTP request to use to submit the form; possible values POST And GET.

Comment

The main difference between the POST and GET methods is the way information is transferred. In the GET method, parameters are passed through the address bar, i.e. essentially in the HTTP request header, while in the POST method the parameters are transmitted through the body of the HTTP request and are not reflected in any way in the address bar.

$text = nl2br($_POST["mytext"]);
?>

Task: Suppose you need to create a drop-down list with years from 2000 to 2050.
Solution: You need to create an HTML form with a SELECT element and a PHP script for processing the form.

Discussion:

First, let's create two files: form.html And action.php. In file form.html will contain an html form with a drop-down list. Moreover, the values ​​in the list can be specified in two ways:

I. Manual data entry:

II. Entering data through a loop:

As you can see, the second example with a loop is more compact. I think there is no need to provide the handler script for this form, because it is processed exactly the same as a text field, i.e. list values ​​can be retrieved from a superglobal array $_POST.

Description:

Let's create an HTML form to send a file to the server.




This html form contains an element browse, which opens a dialog box for selecting a file to upload to the server. When you press the button "Transfer file", the file is passed to the handler script.

Then you need to write a handler script action.php. Before writing the handler, we need to decide in which directory we will copy the file:

if(isset($_FILES [ "myfile" ])) // If the file exists
{
$catalog = "../image/" ; // Our catalog
if (is_dir($catalog)) // If such a directory exists
{
$myfile = $_FILES [ "myfile" ][ "tmp_name" ]; // Temporary file
$myfile_name = $_FILES [ "myfile" ][ "name" ]; // File name
if(! copy ($myfile, $catalog)) echo "Error copying file". $myfile_name // If the file copy failed
}
else mkdir ("../image/" ); // If there is no such directory, we will create it
}
?>

Comment

If you trust users to upload any files to your server, you need to be extremely careful. Attackers can embed “bad” code into a picture or file and send it to the server. In such cases, you need to strictly control the downloading of files.

This example demonstrates creating a directory and copying a file into that directory onto the server.

I would also like to demonstrate an example with the element checkbox. This element is slightly different from other elements in that if not one of the elements checkbox’a is not selected, then the superglobal variable $_POST will return empty value:


Blue
Black
White

if (!empty($_POST [ "mycolor" ])) echo $_POST [ "mycolor" ]; // If at least 1 element is selected
else echo "Select value";
?>




If you have any other questions or something is not clear - welcome to our

All languages ​​of the world have the word "switch"
and only in Russian - “switch”!
Mikhail Zadornov

Today we will talk about such a syntax element PHP, like switches. It should not be confused with an HTML tag , which also creates switches. So,

switch statement

Task: depending on what the user chooses, display the required option. Let's look at the code:

Example 1. HTML page with a form:

Social survey

Enter your grade in Russian:



And here is the handler code:

Example 2. Form handler (cup6.php file):

if (! $score ) (
echo();
) elseif ($score == 1 ) (
echo ("Horror!" );
) elseif ($score == 2 ) (
echo ("Fail %-(" );
) elseif ($score == 3 ) (
echo ("Ud...");
) elseif ($score == 4 ) (
echo("Okay");
) elseif ($score == 5 ) (
echo("Great!");
) else (
echo ( "Interesting assessment...");
}
?>

I think that you all easily understood the code and understood how it works. Generally speaking, this code is correct, and there are no errors in it (it seems...), but it is very inconvenient: a whole bunch of conditions, it’s quite difficult to keep track of where everything is. What to do? Now we will need the switch switch.

The switch looks like this:

Example 3. Appearance of the switch:

switch (expression) (
case value1 :
// commands that are executed if expression = value1
break; // optional
case value2 :
// commands that are executed if expression = value2
break; // optional
...
default:
// commands that are executed if not found
// no matches
break; // optional
}

How this whole block works:

  1. The value of the expression is calculated.
  2. The value of the expression is checked against the value 1. If they are equal, then the code located after the case value1 command is executed:
  3. If the value of the expression is not equal to the value 1, it is checked against the value 2, 4, and so on.
  4. If no matches were found, the code in the default block is executed.

The default block is optional: it can be omitted.

break() command; after each block of code is needed so that after the code has been executed, all subsequent cases are simply skipped.

Now let's change the code of the second example to be more convenient! For example, like this:

Example 4. Modified code from example 2 (file cup6.php):

switch ($score) (
case 0 :
echo ( “You still need to enter a rating...”);
break;
case 1 :
echo ("Horror!" );
break; case 2:
echo ("Fail %-(" );
break;
case 3:
echo ("Ud...");
break;
case 4:
echo("Okay");
break;
case 5:
echo("Great!" );
break;
default:
echo (
"Interesting assessment...");
}
?>

Like this. Simple and convenient. The results of this work are depicted in the figures:

Before After

Connection

It is often much more convenient to break the code into several parts and display them in the right places. To include the contents of one file in another file, we can use two commands: require(); and include();

require()

Team appearance:

Example 6. Appearance of the require() command:

require("filename" );

Before starting the script PHP finds all require() commands; And replaces them (commands) with the contents of the specified file.

The replacement only happens once before the script starts running, so you won't be able to include the require() command; into the body of the loop!

The file that you include with the require() command; may contain PHP-code. This code will be executed.

include()

Team appearance:

Example 7. Appearance of the include() command:

include("filename");

include() command; inserts and executes the contents of the specified file, and this happens during script execution every time the inlcude() command is encountered; .

Despite the obvious similarity, the require(); commands and include(); are seriously different: the require() command; is executed once before the script starts executing, and the inlcude(); every time it occurs in the code and you can easily put the command inlcude(); into cycles.

How this can help in life: on almost every site there are parts of the page that do not change throughout the site - for example, the header (header) of the site and the footer (bottom) of the site. In order not to reprint them on each page, you should put them in two separate files (for example, header.php and footer.php) and include them as needed.

The code for such a page might look, for example, like this:

Example 8. Connecting the header and bottom of the page from external files:

require("header.php" ); // here is the text of the current page
require("footer.php" );
?>

Using require() in this example is preferable since we are only including these files once.

require_once() and inlcude_once()

If you need to make sure that a certain file is included in the code only once, then instead of require(); and include(); need to use require_once(); and require_once();

Look at the examples:

Example 9. Double use of include_once():

include_once("top.php" );
// some code here include_once("top.php" );
?>

In the previous example, the top.php file will only be included once in the page code, even though there are two commands in the code.

That's it...

Well, that seems to be all I wanted to tell you for today. Will there be any problems with PHP- write to me, I will help!

As homework: make a simple calculator. As a hint, here is a picture for you:

And here’s another thing: is it worth setting such “homework” at all?

It's no secret that the most common way an HTML page interacts with a website is a form. The form (that is, the HTML element formed by the form tag) is also used by free email services, electronic stores and many other types of sites.

Processing simple forms using PHP is easy. However, from time to time there is a need to process a form containing several fields of the same type, despite the fact that their number can vary over a wide range and their number is not known in advance. For such cases, PHP provides for processing fields of the same type as an array of values.


Let's take a closer look at the options for different types of fields.

Text fields

In this article, text fields refer to elements created by input tags with a type parameter value of text and a textarea tag. It is easiest to organize the processing of a form consisting of several such fields. The listing below shows the HTML markup for such a form.






As you can see from the listing, the names for the form elements, from a PHP point of view, are array elements. Therefore, the PHP script that will process this form will treat all the many text fields of this form as a single array. Individual elements can be accessed by index or enumerated using the list and each commands, as in the following example.

n"; ?>

Switches

In this article, checkboxes are elements created by input tags with a type parameter value of checkbox . The form for using a variable number of “switches” is built in exactly the same way. Note that the choice of the specific radio button value (that is, the value of the value property) is not important. An example is shown in the listing below:






However, the processing of such a form differs from the processing described for text fields. In this case, it is necessary to determine whether a site visitor has turned on this or that switch. If enabled, then the corresponding array element exists; if not, then it is missing. The following listing is an example PHP script that prints the enabled radio buttons:

Radio buttons

Before describing the processing of radio buttons, it is necessary to remember how they work. The essence of radio buttons (elements created by input tags with the value of the type parameter equal to radio ) is that by selecting one button, the user automatically deselects another button from the same set. Buttons are combined into a set very simply: all buttons in the set have the same name.

But the values ​​(that is, the value parameters) of the buttons in the set are different. And the value of the selected button with the name of the set will be sent to the site. Just as with text fields and radio buttons, the names of sets of radio buttons should be formatted as names of array elements in PHP. An example of such a form is given in the following listing:

// first set of buttons
// second set of buttons
// third set of buttons

Processing radio buttons combines the ideas of using both text fields and radio buttons in processing. If the author of the html page has not set a default value, and the user has not selected a specific button in the set of radio buttons, then this element will not be in the array (as for radio buttons).

If the button is selected, then the corresponding array element will contain its value (as for text fields). Below is an example listing that processes a form with multiple sets of radio buttons.

n"; ?>

Thus, there is nothing complicated in processing complex shapes.