How to obtain form data in php

Source: Internet
Author: User
Tags comments html form http post php print php tutorial

This is a basic php Tutorial. We want to show you how to use php functions to obtain form data. In php, three methods are provided to obtain data: post, get, and request, the most common methods are $ _ POST and $ _ GET.
$ _ REQUEST variable
The $ _ REQUEST variable in PHP contains $ _ GET, $ _ POST, and $ _ COOKIE content.

The $ _ REQUEST variable of PHP can be used to obtain the results of form data sent through the GET and POST methods.


Instance

 

<Html>
<Head>
<Title> An HTML Form that callitself </title>
</Head>
<Body>
<Div>
<? Php
If (! Empty ($ _ POST ['Guess ']) {
Print "last guess:". $ _ POST ['Guess '];
}
?>
<Form method = "post" action = "<? Php print $ _ SERVER ['php _ SELF ']?> ">
<P>
Type your guess here: <input type = "text" name = "guess"/>
</P>
</Form>
</Div>
</Body>
</Html>

Because the table provides a large amount of data, we try to use post to obtain it. More complex example 2:
 
Instance 2

<? Php

$ Self = $ _ SERVER ['php _ SELF '];
$ Username = $ _ POST ['username'];
$ Useraddr = $ _ POST ['useradd'];
$ Comments = $ _ POST ['comments'];
$ Sent = $ _ POST ['sent'];

$ Form = "<form action =" $ self "method =" post "> ";
$ Form. = "Name: <input type =" text "name =" username "";
$ Form. = "size =" 30 "value =" $ username "> ";
$ Form. = "Email: <input type =" text "name =" useraddr "";
$ Form. = "size =" 30 "value =" $ useraddr "> ";
$ Form. = "Comments: <textarea name =" comments "> ";
$ Form. = "$ comments </textarea> <br/> ";
$ Form. = "<input type =" submit "name =" sent "value =" Send Form "> ";
$ Form. = "</form> ";

If ($ sent)
{
$ Valid = true;

If (! $ Username)
{$ Errmsg. = "Enter your name... <br/>"; $ valid = false ;}

If (! $ Useraddr)
{$ Errmsg. = "Enter your email address... <br/>"; $ valid = false ;}

If (! $ Comments)
{$ Errmsg. = "Enter your comments... <br/>"; $ valid = false ;}

$ Useraddr = trim ($ useraddr );
$ _ Name = "/^ [-! # $ % & '* +./0-9 =? A-Z ^ _ '{|} ~] + ";
$ _ Host = "([-0-9A-Z] +.) + ";
$ _ Tlds = "([0-9A-Z]) {2, 4} $/I ";
If (! Preg_match ($ _ name. "@". $ _ host. $ _ tlds, $ useraddr ))
  {
$ Errmsg. = "Email address has incorrect format! <Br/> ";
$ Valid = false;
  }
}

If ($ valid! = True)
{
Echo ($ errmsg. $ form );
}
Else
{
$ To = "php@h.com ";

$ Re = "Feedback from $ username ";

$ Msg = $ comments;

$ Headers = "MIME-Version: 1.0rn ";
$ Headers. = "Content-type: text/html ;";
$ Headers. = "charset =" iso-8859-1 "rn ";

$ Headers. = "From: $ useraddr rn ";

If (mail ($ to, $ re, $ msg, $ headers ))
{Echo ("Your comments have been sent-thanks $ username ");}

}
?>

You can process the data submitted by the user. I only talked about $ _ POST.

$ _ POST variable
The $ _ POST variable is an array with the variable name and value sent by the http post method.

The $ _ POST variable is used to collect the values in the form from method = "post. The information sent from a form with the POST method is invisible to anyone (not displayed in the address bar of the browser) and there is no limit on the amount of information sent.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.