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.
代码如下 | 复制代码 |
<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 |
代码如下 | 复制代码 |
//通过判断按钮的变量名是否在$_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 |