Home > Backend Development > PHP Tutorial > PHP tutorial form submission example_PHP tutorial

PHP tutorial form submission example_PHP tutorial

WBOY
Release: 2016-07-13 17:12:56
Original
911 people have browsed it

A super simple form submission example of PHP tutorial for beginners. Friends in need can refer to it. Below we will create a complex form with the code shown below.

Copy after login
Copy after login
 代码如下 复制代码
<form action="someform.php" method="post"><br><table width="541" border="0"><br>  <tr><br>    <td width="26%">姓名:</td><br>    <td width="74%"><input type="text" name="username" value="raymond" id="username"/></td><br>  </tr><br>  <tr><br>    <td>密码:</td><br>    <td><input type="password" name="password" maxlength="10" id="password"/></td><br>  </tr><br>  <tr><br>    <td>年龄:</td><br>    <td><br>      <select name="age"><br>      <option value=">16">小于16</option><br>      <option value="16-30" selected>16-30</option><br>      <option value="31-50">31-50</option><br>      <option value="51-80">51-80</option><br>      </select><br>    </td><br>  </tr><br>  <tr><br>    <td valign="top">自我介绍:</td><br>    <td><textarea name="intro" rows="3" cols="50" id="intro">请输入您的自我介绍</textarea></td><br>  </tr><br>  <tr><br>    <td><br /><br>体育爱好: </td><br>    <td><input type="radio" name="fave_sport" value="tennis" checked><br>网球<br>    <input type="radio" name="fave_sport" value="football"><br>足球<br><input type="radio" name="fave_sport" value="baseball"><br>篮球<br><input type="radio" name="fave_sport" value="polo"><br>保龄球 </td><br>  </tr><br>  <tr><br>    <td> 开发语言:</td><br>    <td><input name="from" type="hidden" id="from" value="注册表单"><br>      <input type="checkbox" name="languages[]" value="php" checked id="languages[]"><br>php<br><input type="checkbox" name="languages[]" value="java" id="languages[]"><br>java<br><input type="checkbox" name="languages[]" value="perl" id="languages[]"><br>perl<br><input type="checkbox" name="languages[]" value="cpp" id="languages[]"><br>c++<br><input type="checkbox" name="languages[]" value=".net" id="languages[]"><br>.NET<br><input type="checkbox" name="languages[]" value="delphi" id="languages[]"><br>delphi </td><br>  </tr><br>  <tr><br>    <td valign="top"><br><br>      <label> 开发工具:</label></td><br>    <td><select name="develop_ide[]" size="5" multiple id="develop_ide[]"><br>      <option value="ZDE" selected>Zend Studio</option><br>      <option value="Eclipse">Eclipse</option><br>      <option value="Editplus">Editplus</option><br>      <option value="Ultraedit">Ultraedit</option><br>      <option value="Other">Other</option><br>      </select></td><br>  </tr><br>  <tr><br>    <td valign="top"> </td><br>    <td><input type="submit" name="btn_submit" value="提交"/></td><br>  </tr><br></table><br></form>
Copy after login
This form includes common form elements: single-line text box, multi-line text box, single option (radio), multiple options (checkbox), and multi-select menu. Detailed explanation is provided below.

maxlength is an attribute associated with the password text box, which limits the maximum length of the user's password to 10 characters.

The age list box is a list menu with its own values ​​for selection under its named properties. Selected is a specific attribute selection element. If an option is attached with this attribute, the item will be listed as the first item when displayed.

The content in the intro text box displays text, row and column widths according to rows and cols.

fave_sport is a group of radio buttons (radio). We need to name the elements according to the group. For example, this group of radio buttons is called fave_sport. The user can only select one, and there is only one value in the sending script.

Like the single option, all multi-option members must also have attributes with the same name, and brackets [] need to be added to the attribute name, so that the value of the multi-option is sent to PHP in the form of an array, languages ​​is this form.

The checked tag refers to a certain value in single option and multi-option, which is selected by default. The display screen of the above form is shown in Figure 5-3. Figure 5-3 Because the form form in the above HTML uses the POST method to transfer data, the data submitted by the user will be saved in the super global array of $_POST or $_REQUEST. We can process the submitted data based on the value in the $_POST array. Submit the data in the above form to the someform.php script. The processing logic of the script is as follows:
Copy after login
Copy after login
 代码如下 复制代码
//通过判断按钮的变量名是否在$_POST中定义,如果有表示该表单已提交<br>if(isset($_POST["btn_submit"])){<br>if (empty($_POST['username'])){<br>echo "您没有输入用户名";<br>exit(0);<br>}<br>if (empty($_POST['password'])){<br>echo "您没有输入密码: ";<br>exit(0);<br>}<br>echo "您的用户名: ".$_POST['user_name']."<br>";<br>echo "您的密码(明文): ".$_POST['password']."<br>";<br>echo "您的年龄: ".$_POST['age']."<br>";<br>if (!empty($_POST['languages'])){<br>echo "您选择的语言为:";<br>//处理用户选择兴趣的checkbox按钮产生的数组<br>foreach ($_POST['languages'] as $lang){<br>echo $lang. "  ";<br>}<br>} else {<br>echo "您没有输入任何兴趣爱好";<br>}<br>if (!empty($_POST['develop_ide'])){<br>echo "您使用的开发工具为:";<br>//处理用户多选开发工具菜单产生的数组<br>foreach ($_POST['develop_ide'] as $ide){<br>echo $ide. "  ";<br>}<br>} else {<br>echo "您没有选择开发工具";<br>}<br>echo "您的自我介绍: ".nl2br($_POST['intro'])."<br />";//nl2br(),在字符串中的每个新行 (n) 之前插入 HTML 换行符 (<br />)<br>";<br>echo "网页隐藏值(通过hidden标签值传递): ".$_POST['from']."<br>";<br>}<br>?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629250.htmlTechArticleA super simple form submission example of PHP tutorial for beginners. Friends in need can refer to it. Below we will create a complex form with the code shown below. The code is as follows Copy code...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template