How to obtain form data in php

Source: Internet
Author: User
This is a basic php getting started tutorial. we want to tell 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 method is $ _ POST and $ _ GET. check the $ _ REQUEST variable and PHP $ _ REQUEST.

This is a basic php getting started tutorial. we want to tell 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 method is $ _ POST and $ _ GET. check the $ _ 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 1:

  1.  
  2.  
  3. An HTML Form that callitself 
  4.  
  5.  
  6.  
  7. If (! Emptyempty ($ _ POST ['Guess ']) {
  8. Print "last guess:". $ _ POST ['Guess '];
  9. }
  10. ?>
  11.  
  12.  
  13.  
  14.  

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

Example 2:

  1.  
  2. $ Self = $ _ SERVER ['php _ SELF '];
  3. $ Username = $ _ POST ['username'];
  4. $ Useraddr = $ _ POST ['useradd'];
  5. $ Comments = $ _ POST ['comments'];
  6. $ Sent = $ _ POST ['sent'];
  7.  
  8. $ Form = "";
  9.  
  10. If ($ sent)
  11. {
  12. $ Valid = true;
  13.  
  14. If (! $ Username)
  15. {$ Errmsg. = "Enter your name... "; $ Valid = false ;}
  16.  
  17. If (! $ Useraddr)
  18. {$ Errmsg. = "Enter your email address... "; $ Valid = false ;}
  19.  
  20. If (! $ Comments)
  21. {$ Errmsg. = "Enter your comments... "; $ Valid = false ;}
  22.  
  23. $ Useraddr = trim ($ useraddr );
  24. $ _ Name = "/^ [-! # $ % & '* +./0-9 =? A-Z ^ _ '{|} ~] + ";
  25. $ _ Host = "([-0-9A-Z] +.) + ";
  26. $ _ Tlds = "([0-9A-Z]) {2, 4} $/I ";
  27. If (! Preg_match ($ _ name. "@". $ _ host. $ _ tlds, $ useraddr ))
  28. {
  29. $ Errmsg. = "Email address has incorrect format! ";
  30. $ Valid = false;
  31. }
  32. }
  33.  
  34. If ($ valid! = True)
  35. {
  36. Echo ($ errmsg. $ form );
  37. }
  38. Else
  39. {
  40. $ To = "php@h.com ";
  41.  
  42. $ Re = "Feedback from $ username ";
  43.  
  44. $ Msg = $ comments;
  45.  
  46. $ Headers = "MIME-Version: 1.0rn ";
  47. $ Headers. = "Content-type: text/html ;";
  48. $ Headers. = "charset =" iso-8859-1 "rn ";
  49.  
  50. $ Headers. = "From: $ useraddr rn ";
  51.  
  52. If (mail ($ to, $ re, $ msg, $ headers ))
  53. {Echo ("Your comments have been sent-thanks $ username ");}
  54.  
  55. }
  56. ?>

We can perform some processing on the data submitted by the user. we 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" and send information from the form with the POST method, it is invisible to anyone (not displayed in the address bar of the browser), and there is no limit on the amount of sent information.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.