[php笔记]html的表单

2023-05-16

最近刚完成一个asg。是一个简单的学生,课程的管理系统。基于web的,利用LAMP(linux+apache+mysql+php)实现的一个小的系统。由于之前没太接触过php。这次开发用了3天。途中也遇到很多麻烦和bug。所以现在做一下笔记,希望能对和我一样的初学者一些帮助。

由于是这个系统有一个功能,就是管理员(admin)可以添加,或删除一个学生,或一门课。这里就用到了表单。如下是一个表单,包含了几种常用的元素:

<!doctype html>
<html>
  <head>            
    <title> php and form </title>
  </head>                     
  <body>                
    <form action="process_form.php" method="post">
      Input text<input type="TEXT" name="text[]"/>
      <br/>                   
      Input text<input type="TEXT" name="text[]"/>                   
      <br/>                                                          
      Input password<input type="PASSWORD" name="password"/>         
      <br/>                                                          
      Radio buttons:<input type="RADIO" name="radio" value="r1"/>r1  
                    <input type="RADIO" name="radio" value="r2"/>r2  
      <br/>                                                          
      Check box:<input type="CHECKBOX" name="checkbox[]" value="c1"/>c1
                <input type="CHECKBOX" name="checkbox[]" value="c2"/>c2   
                <input type="CHECKBOX" name="checkbox[]" value="c3"/>c3
      <br/>                                                          
      Select: <select name="select">
                <option value="s1">s1</option>                       
                <option value="s2">s2</option>                       
                <option value="s3">s3</option>                       
              </select>
      <br/>     
      Text area:    <br/>
      <textarea name="textarea">t1</textarea>                        
      <br/>                                                          
      <input type="SUBMIT" value="submit"/>                          
    </form>
  </body>
</html>
对应的下面是处理的php代码
<?php
//create short name 
$text=$_POST['text'];
$password=$_POST['password']; 
$radio=$_POST['radio']; 
$checkbox=$_POST['checkbox'];
$select=$_POST['select'];
$textarea=$_POST['textarea']; 
      
echo 'text: '.$text[1].'<br/>';                                      
echo 'password: '.$password.'<br/>';
echo 'radio: '.$radio.'<br/>';                                       
echo 'select: '.$select.'<br/>';
echo 'textarea: '.$textarea.'<br/>';
     
foreach($checkbox as $box) {
    echo 'checkbox: '.$box.'<br/>';
}               
echo $checkbox[0];                                                   
?>

php通过$_POST['xxx']。取得form中name='xxx'的值。对于checkbox这种可能会有多个value的情况,在命名时要加上[]。即name='xxx[]';这样这个value就会当成array传给php官方的手册是这样讲的:To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this:
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" />

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[php笔记]html的表单 的相关文章

随机推荐