php form in the form

Form processing

  The concept of form is very common in life, just like the questionnaire, others put the questionnaire sent to you, you are asked to fill out questionnaires shining, after the bin and then fill out the questionnaire sent to someone else, someone else will so as to achieve a information needed to pass a way of others.
  Most of the pages are showing the role of traditional data, it is to convey information to the user. In the modern Web development, we attach great importance to information exchange, so the form can be seen everywhere, but formally become pages, or exactly the same nature. However, any primary role is to collect user information specified.

1. Use basic form

HTML tag has a dedicated data submitted: <form>, you can easily collect user input via the tag.

form tag has two required attributes: 
  Action: form submission address (fill out, and to whom) 
  Method,: in what way the form is submitted

For example, we need to collect user input user name and password on the login screen:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF‐8">
  <title>登录</title>
</head>
<body>
  <form action="login.php" method="post">
    <div>
      <label for="username">用户名</label>
      <input type="text" id="username" name="username">
    </div>
    <div>
      <label for="password">密码</label>
      <input type="password" id="password" name="password">
    </div>
    <button type="submit">登录</button>
  </form>
</body>
</html>

According to the current situation, the first time the user requests to get this form page, fill out the form, click on the login, the form is automatically sent to login.php, the remaining question is to consider how to get users to come in login.php submitted Content.

PHP has three superglobals designed to obtain a form submission: 
    $ _GET: GET way to get content for submission of 
    $ _POST: POST method for obtaining content submitted 
    $ _REQUEST: for a GET or POST submission Content

With $ _POST or $ _REQUEST you can get to the contents of form submission:

? < PHP
 //  get the form submitted by a user name and password 
echo  'User name:'.  $ _REQUEST [ 'username' ];
 echo  'Password:'.  $ _REQUEST [ 'password'];

1.1 The author addresses

  action refers to the address submitted this form filled out after clicking submit, what request send the requested address Yes.
  From the point of view of ease of maintenance, generally our most common are submitted to the current file, and then determine whether it is in the current file request form submission:

<? PHP
 IF  ( $ _SERVER [ 'REQUEST_METHOD'] === 'the POST' ) {
   //  form submission request 
}

In addition, it is recommended to use $ _SERVER [ 'PHP_SELF'] dynamic access path to get the current page, so do not rename the file or because the site directory structure adjustment and modify the code:

<! - write death action addresses, rename the file when you need to modify the code -> 
<form action = "/ foo / login.php"> 
  <! - ... -> 
</ form> 
<! - by ` $ _SERVER [ 'PHP_SELF']` get the path, you can easily avoid this problem -> 
<form Action = "<PHP echo?  $ _SERVER [ 'PHP_SELF'];?>"> 
  <- ..! . -> 
</ form>

1.2. Submission

method can be used to set the way the form is submitted, the current submission as we know it is the most common form two kinds: GET and POST.
  In effect, both of which can submit data to the server, but from the realization of the principle submitted two are very different:
GET
  form data is passed to the server by the URL parameter?
  May be in the address bar see the contents of submitted
  data length is limited, because of the limited length of the URL (2000 characters)
POST
  form data is passed to the server by requesting the body, we do not see the interface
  can submit any type of data, including files
  since the interface the invisible, the browser is not stored, it is more secure
  as to the circumstances under which way should be used, this requires a combination of business scenarios and characteristics of each of these two methods to determine, there is no absolute answer, we can only give some principles :
  never to send passwords or other sensitive information can not be used GET! ! !
  The request should think clearly in the end the main thing is to pick up, or to send something

2. Common form elements processing

As a class of elements in a form element text field text box, we are directly attribute value of the element name as a key value as the user to fill in information, sent to the server. But there are some elements in the form of special form elements need to be considered separately:

2.1 radio button

<! - that one will eventually submit the selected value -> 
<the INPUT of the type = "Radio" name = "Gender" value = "MALE"> 
<the INPUT of the type = "Radio" name = "Gender" value = "female">

2.2. Check button

<-! Value is not set value of the checkbox is checked submitted ON -> 
<the INPUT of the type = "checkbox" name = "Agree"> 
<-! Set checkbox selected value is the value of the submitted value -> 
< input type = "checkbox" name = "agree" value = "true">

If you need to submit multiple selected items at the same time, you can keep up with the [] behind the name attribute:

https://php.net/manual/zh/faq.html.php#faq.html.arrays

<input type="checkbox" name="funs[]" id="" value="football">
<input type="checkbox" name="funs[]" id="" value="basketball">
<input type="checkbox" name="funs[]" id="" value="world peace">

Eventually submitted to the server, received over $ _POST is an indexed array.

2.3. Selection box

<the SELECT name = "Subject"> 
  <-! Set value submitted value -> 
  <the Option value = "1"> Language </ the Option> 
  <-! there is no set value submitted innerText -> 
  <the Option> mathematics </ Option> 
</ SELECT>

2.3. File Upload

type attribute can submit files (upload) for the file input element through the form, the server can get the PHP file information uploaded by $ _FILES.

<? PHP
 //  0 If you select a file _FILES $ [ 'File'] [ 'error'] => 
// Detailed error code Explanation: http: //php.net/manual/zh/features.file-upload. errors.php 
IF  ( $ _FILES [ 'file'] [ 'error'] === 0 ) {
   //  the PHP in the client will automatically receive files uploaded to a temporary directory 
  $ temp_file  =  $ _FILES [ 'file'] [ 'tmp_name' ];
   //  we just need to save the file to our designated upload directory 
  $ target_file  = '../static/uploads/'.  $ _FILES [ 'file'] [ 'name' ];
   IF  ( move_uploaded_file ( $ temp_file ,  $target_file)) {
    $image_file = '/static/uploads/' . $_FILES['file']['name'];
  }
}

$ _FILES associative array is also a key for the form of the name, as follows:

array(1) {
  ["avatar"]=>
  array(5) {
    ["name"]=>
    string(17) "demo.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(27) "C:\Windows\Temp\php786C.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(29501)
  }
}

 

Guess you like

Origin www.cnblogs.com/wjw9/p/10962998.html