Accept data from php form. PHP scripts for processing HTML forms


or how to send a message by e-mail using an HTML form

You have your own website and want to receive letters or messages, questions, advice or just wishes by email from your users, then this lesson is for you!

How to send a message by e-mail

We need the following files:

  1. form.html - page with the form.
  2. form_processing.php - file with the script, processing HTML form.

Let's look at the form.html code:

Processing HTML Forms Using PHP



Your name:


Email:


Subject:


Message:





HTML form contains fields for entering the user's name, email address, message subject, message text and the "Send" button, when clicked, the information is sent for processing to PHP script to the form_processing.php file.

The hypertext transport protocol method has two values: get (default) and post. The post method is used more often because it allows large amounts of data to be transferred. All values ​​passed to the processing script via the post method are stored in the associative array $_POST (this array is natively built into the PHP interpreter), which consists of the $_POST variables, where name is the actual name of the input field - the value of the name="" attribute:

Let's create a processor file form_processing.php:

/* We check the entered data and protect it from hostile ones
scripts */
$your_name = htmlspecialchars($_POST["your_name"]);
$email = htmlspecialchars($_POST["email"]);
$tema = htmlspecialchars($_POST["tema"]);
$message = htmlspecialchars($_POST["messages"]);
/* Set the recipient’s e-mail */
$myemail = " [email protected]" ;
/* Check if required input fields are filled in using check_input
function */
$your_name = check_input($_POST["your_name"], "Enter your name!");
$tema = check_input($_POST["tema"], "Specify the subject of the message!");
$email = check_input($_POST["email"], "Enter your email!");
$message = check_input($_POST["message"], "You forgot to write a message!");
/* Check if the e-mail is written correctly */
if (! preg_match ( "/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email ))
{
show_error( "
Email address does not exist"
);
}
/* Create a new variable by assigning a value to it */
$message_to_myemail = "Hello!
Your contact form has sent a message!
Sender name: $your_name
Email: $email
Message text: $message
End"
;
/* Send a message using the mail() function */
$from = "From: $yourname<$email>\r\n Reply-To: $email \r\n";
mail ($myemail, $tema, $message_to_myemail, $from);
?>

Your message has been successfully sent!


To Home >>>


/* If errors were made when filling out the form, it will work
the following code: */
function check_input ($data, $problem = "")
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen ($data) == 0)
{
show_error($problem);
}
return $data ;
}
function show_error ($myError)
{
?>


Please fix the following error:





exit();
}
?>

Code section:

— will display the specified text if the fields HTML forms were filled out correctly. index.php - the main page of your site.

Code section:

- indicates the nature of the mistake made.

Variable value:

$from = "From: $yourname<$email>\r\n Reply-To: $email \r\n";
?>

— will automatically display the user’s e-mail in the required line when you write a response.

HTML forms are complex interface elements. They include different functional elements: input fields And

You can process forms without worrying about the actual field names.

To do this, you can use (depending on the transfer method) the associative array $HTTP_GET_VARS or $HTTP_POST_VARS . These arrays contain name/value pairs for each element of the submitted form. If you don't care, you can use the associative array $_REQUEST .

Example 6

Handle arbitrary input regardless of transmission method $value) echo "$key == $value
"; ?>

Example 7: Handling a button click using the "@" operator.

">

Using the header() function, sending the browser a "Location" header, you can redirect the user to a new page.

For example:

Transferring a file to the server. Upload the file. UpLoad

PHP allows you to transfer files to the server. An HTML form intended to submit a file must contain the enctype="multipart/form-data" argument.

In addition, in the form, before the field for copying a file, there should be a hidden field named max_file_size. The maximum size of the transferred file should be written to this hidden field (usually no more than 2 MB).

The file transfer field itself is a regular INPUT element with a type="file" argument.

For example:

" method="post">

After the file is transferred to the server, it is given a unique name and stored in the temporary files directory. The full path to the file is written to a global variable whose name matches the name of the field for transferring this file. In addition, PHP stores some additional information about the transferred file in other global variables:

Example 8

Processing the transferred file "; echo "name: ".$_FILES["userfile"]["name"]."
"; echo "size: ".$_FILES["userfile"]["size"]."
"; echo "type: ".$_FILES["userfile"]["type"]."
"; } ?>
" method="post">

Examples of uploading files to the server

If problems arise with the server transcoding the downloaded file, the symbol with the code 0x00 replaced with a space (character with code 0x20), add to file httpd.conf from the Apache directory (/usr/local/apache) the following lines.

CharsetRecodeMultipartForms Off

Interactive sites accept input from users. One common way to receive input is through forms.

In this lesson we will see how to create a form and process input on the server.

When creating a form, two important attributes are involved: action And method.

action Used to enter the URL where the form is being submitted. This could be a PHP file that processes the input. method Can be "post" or "get", which are different methods of transferring data. You don't need to go into the differences between these methods just yet; The "get" method sends the data via a URL, and the "post" method sends it as a block of data via the standard input service (STDIN). In the last lesson we took, we saw how data is retrieved through a URL using $_GET. In this tutorial we will look at how data sent through a form using the "post" method is obtained.

HTML page with form

The page with the form does not have to be a PHP file (but it can be). It doesn't even have to be on the same site as the file that receives the data.

In our first example, we'll look at a very simple form with a single text field:

Form

Enter your name

The browser will display the form:

Now comes the fun part: retrieving and processing data using PHP.

Requesting form data using PHP

If you need to request data sent through a form (using the post), you use $_POST :

$_POST["fieldname"];

which will return the value of the form field. Let's try this with an example.

First, let's create a page with a form as before. Next, let's create a PHP page (handler) "handler.php" (note that this is the page name that we wrote in the attribute action in our

).

The "handler.php" file will contain:

Form echo "

"; ?>

User input and conditions

In the following example we will try to use user input to create conditions. First we need a form:

Form

What is your name:

Your favorite color: Red Green Blue

In the browser it will be like this:

Now we use these inputs to create a page that automatically changes the background color based on user input. This is done by creating a condition (see Lesson) that uses data entered by the user into the form.

$strHeading = "

Hello, " . $_POST["username"] . "

"; switch ($_POST["favoritecolor"]) ( case "r": break; case "g"; break; case "b": break; default: break; ) ?> Form

The background will be white unless the user specifies a preferred color on the form. This is achieved by setting the value default(default), which is applied if none of the conditions are met.

But what happens if the user does not indicate his name? Then the title will only say "Hello." Let's create an additional condition to change this option.

$strUsername = $_POST["username"]; if ($strUsername != "") ( $strHeading = "

Hello, " . $_POST["username"] . "

"; ) else ( $strHeading = "

Hello Stranger!

";
} switch ($_POST["favorite color"]) ( case "r": $strBackgroundColor = "rgb(255,0,0)"; break; case "g"; $strBackgroundColor = "rgb(0,255,0)"; break; case "b": $strBackgroundColor = "rgb(0,0,255)"; break; default: $strBackgroundColor = "rgb(255,255,255)"; break; ) ?> Form

In the above example we are using conditions for checks information from the user. In this case, it is not so important if the user does not specify a name. But as your code becomes more sophisticated, it's vital that you account for the possibility that the user doesn't fill out the forms at all.

Example: Contact Information Form

Based on your existing knowledge of PHP and forms, you can create a contact information form using the mail function, which has the following syntax:

Mail (where, subject, message);

First we create a simple HTML form:

Contact information form

Contact information form

Subject:

Message:

You will then need a PHP script to send the user input:

Functions // Recipient (change to your email address) $strEmail = " [email protected]"; // Get user inputs $strSubject = $_POST["subject"]; $strMessage = $_POST["message"]; mail($strEmail,$strSubject,$strMessage); echo "Mail Sent."; ?>

Note that the example only works if you have access to the mail server. This is not the case by default in XAMPP and most free hosts. So, some hosts may require a form header, which is done with an additional parameter:

Mail ("[email protected]", "Test", "This is a test message", "From: [email protected]");







2024 gtavrl.ru.