简体   繁体   English

PHP中的表单处理

[英]Form processing in PHP

I have three questions about some code : 关于某些代码,我有三个问题:

  1. How does isset($_POST['submit']) knows when the submit button has been clicked. isset($_POST['submit'])知道何时单击了提交按钮。 I dont get the the mechanics behind this ? 我不了解背后的机制吗?
  2. Why is each input element using a name with a value of artist[] with the square brackets like that ? 为什么每个输入元素都使用一个具有artist[]值且带有方括号的名称? Are we defining and array inside HTML ? 我们是在HTML中定义数组吗?
  3. I was told that the value of name given to an input element has to match the name given to the $_POST variable. 有人告诉我输入元素的名称值必须与$_POST变量的名称相匹配。 But in this case $_POST is using artist with without the square brackets. 但是在这种情况下, $_POST使用的artist没有方括号。 Whats going on here ? 这里发生了什么 ?

.

 <html>

 <head></head>
 <body>

 <?php

 // check for submit
  if (!isset($_POST['submit'])) {

   // and display form
   ?>

 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="checkbox" name="artist[]" value="Bon Jovi">Bon Jovi
    <input type="checkbox" name="artist[]" value="N'Sync">N'Sync
    <input type="checkbox" name="artist" value="Boyzone">Boyzone
    <input type="checkbox" name="artist" value="Britney Spears">Britney Spears
    <input type="checkbox" name="artist" value="Jethro Tull">Jethro Tull
    <input type="checkbox" name="artist" value="Crosby, Stills & Nash">Crosby, Stills & Nash
    <input type="submit" name="submit" value="Select">
</form>

 <?php

 } else {

     // or display the selected artists
     // use a foreach loop to read and display array elements
     if (is_array($_POST['artist'])) {

         echo 'You selected: <br />';

         foreach ($_POST['artist'] as $a) {
             echo "<i>$a</i><br />";
         } 

     } else {

         echo 'Nothing selected';

     }
}
?>
</body>
</html>

1.How does isset($_POST['submit']) know when the submit button has been clicked. 1. isset($ _ POST ['submit'])如何知道何时单击了提交按钮。

Clicking the submit button causes the browser to submit the form (and include the name/value of the clicked submit button in the data). 单击提交按钮会使浏览器提交表单(并在数据中包含单击的提交按钮的名称/值)。

Submitting the form creates an HTTP request. 提交表单将创建一个HTTP请求。

The PHP won't run except as a reaction to the server receiving an HTTP request. 除了作为对服务器收到HTTP请求的响应之外,PHP不会运行。

The PHP examines the data to see if the submit buttons name/value in in it. PHP检查数据以查看其中是否包含提交按钮的名称/值。

  1. Why is each input element using a name with a value of "artist[]" with the square brackets like that? 为什么每个输入元素都使用带有方括号的“ artist []”值的名称?

Most form processing libraries will provide a way to access multiple elements with the same name as an array. 大多数表单处理库将提供一种访问与数组同名的多个元素的方法。 PHP's requires that the name ends in [] before allowing that. PHP要求名称必须以[]结尾,然后再允许。

I was told that the value of name given to an input element has to match the name given to the $_POST variable. 有人告诉我输入元素的名称值必须与$ _POST变量的名称相匹配。 But in this case $_POST is using "artist" with without the square brackets. 但是在这种情况下,$ _ POST使用的是“艺术家”,没有方括号。 Whats going on here? 这里发生了什么?

PHP's form data parser treats [] as a message to create an array and not as part of the name. PHP的表单数据解析器将[]视为创建数组的消息,而不是名称的一部分。 It is odd. 真奇怪

It does however, allow you to define such things as: 但是,它确实允许您定义以下内容:

<input type="checkbox" name="foo[bar][]" value="1">
<input type="checkbox" name="foo[bar][]" value="1">
<input type="checkbox" name="foo[bar][]" value="1">
<input type="checkbox" name="foo[oat][]" value="1">
<input type="checkbox" name="foo[oat][]" value="2">
<input type="checkbox" name="foo[oat][]" value="3">

And then loop over $_POST['foo'] to get an array of two items which can be looped over in turn. 然后循环$_POST['foo']以得到两个项目的数组,可以依次循环。

  1. $_POST['submit'] is set when the form is submitted and a input field with name "submit" exists. $_POST['submit']表单并且存在名称为“ submit”的输入字段时设置$_POST['submit'] In your case, the <input type='submit'> has a name of "submit". 在您的情况下, <input type='submit'>的名称为“ submit”。 (You can also get the value of the submit button using $_POST['submit'] which holds the value). (您也可以使用保存值的​​$ _POST ['submit']获取提交按钮的值)。

  2. The names have artist[] because it declares an array to be passed as the form is processed. 名称具有artist [],因为它声明了在处理表单时要传递的数组。 You can use it to group values together. 您可以使用它来将值分组在一起。 Usually, it's done by artist[name] , artist[album] etc. which can the be referenced in PHP using _POST['artist']['name'] . 通常,它是由artist[name]artist[album]等完成的,可以在PHP中使用_POST['artist']['name']进行引用。

  3. Sort of follows from 2. Using empty square brackets makes an array without associations. 从2开始进行排序。使用空方括号会使数组不相关。 You'd reference the first field as $_POST['artist'][0] and next $_POST['artist'][1] . 您将第一个字段引用为$_POST['artist'][0] ,下一个$_POST['artist'][1]

When all fields have been given the same name, an array is built and you can reference it in PHP like 3. 给所有字段指定相同的名称后,将构建一个数组,您可以像3一样在PHP中引用它。

  1. The submit button actually submits the form and it's a POST value on it's own. 提交按钮实际上是提交表单,它本身就是一个POST值。 So if it's set it means that the form was submitted. 因此,如果已设置,则表示表单已提交。 Though, it would be better to check if $_SERVER['REQUEST_METHOD'] is equal to 'POST'. 不过,最好检查$_SERVER['REQUEST_METHOD']是否等于'POST'。

  2. Yes, it's defining an "array", but it's wrong because in the last 4 checkboxes it doesn't have the square brackets. 是的,它定义了一个“数组”,但这是错误的,因为在最后四个复选框中没有方括号。

  3. $_POST['artist'] would contain an array with all the checkboxes values (true or false, depending on wheteher they were checked or not respectively). $_POST['artist']将包含一个带有所有复选框值的数组(是或否,取决于是否分别选中它们)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM