一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

html网页中php提交表单与取值代码

时间:2011-06-04 编辑:简简单单 来源:一聚教程网

这两天学了写表单。总结于此,以备忘。
例子一(POST提交表单):
 
view sourceprint?
    
         </code></td> </tr> </tbody> </table> </div> <div> <table> <tbody> <tr> <td><code>        </code><code>Chunkify Form </code></td> </tr> </tbody> </table> </div> <div> <table> <tbody> <tr> <td><code>        </code><code>
    
    
    
"chunkify.php教程" method="POST">
    Enter a word:
    "text" name="word"/>
    How long should be the chunks be?
    "text" name="number"/>
    "submit" value="Chunkify">
    
    
 
view sourceprint?
    
         </code></td> </tr> </tbody> </table> </div> <div> <table> <tbody> <tr> <td><code>        </code><code>Chunkify Word </code></td> </tr> </tbody> </table> </div> <div> <table> <tbody> <tr> <td><code>        </code><code>
    
    
        $word=$_POST['word'];
        $number=$_POST['number'];
        $chunks=ceil(strlen($word)/$number);
        echo "The $number-letter chunks of '$word' are:
n"
;
        for ($i = 0;$i<$chunks;$i++){
            $chunk=substr($word,$i*$number,$number);
            printf("%d: %s
n"
,$i+1,$chunk);
        }
    ?>
    
 
html显示出来的页面。
提交表单后php处理出来的页面。在这个例子中,我输入一个单词,然后给定一个长度,将单词等分成该长度的块。
演示了通过POST方法提交表单。
 


例子二(单选,GET接受表单):

view sourceprint?
"" method="GET">
Select your personality attributes:
<select name="att[]" >

"submit" name="s" value="Record my personality">
  
    if (array_key_exists('s',$_GET)){
        $des = implode(' ', $_GET['att']);
        echo "You have a $des personality.";
    }
?>
 


例子三(多选,GET接受表单):
注意到此时"att[]" multiple>

"submit" name="s" value="Record my personality">
  
    if (array_key_exists('s',$_GET)){
        $des = implode(' ', $_GET['att']);
        echo "You have a $des personality.";
    }
?>
 

 


例子四(复选框checkbox): 同样name="att[]"是告诉GET你传输的是一个数组, checked 则表示该选项为初始默认选择,同样上例,在标签内加入 selected="selected"也可以
让多选初始默认选择。

view sourceprint?
"" method="GET">
Select your personality attributes:
perky"checkbox" name="att[]" value="perky" checked />
morose"checkbox" name="att[]" value="morose" checked />
thinking"checkbox" name="att[]" value="thinking" />
feeling"checkbox" name="att[]" value="feeling" />

"submit" name="s" value="Record my personality">
  
    if (array_key_exists('s',$_GET)){
        echo "
"; 
        print_r($_GET);
        echo "";
        if (is_null($_GET['att'])) exit;
          
        $des = implode(' ', $_GET['att']);
        echo "You have a $des personality.";
    }
?>
 
 

 


例子五(单选框): 注意,同一个选项即可的单选必须name相等

view sourceprint?
男性:
"radio" checked="checked" name="Sex" value="male" />

女性:
"radio" name="Sex" value="female" />


男性:
"radio" checked="checked" name="Se" value="male" />

女性:
"radio" name="Se" value="female" />
  

当用户点击一个单选按钮时,该按钮会变为选中状态,其他所有按钮会变为非选中状态。

 
 
 

 


例子六(stick form):一个表格如何要实现之前输入过的值在页面刷新后仍然存在可以如下

view sourceprint?
  $f = $_POST['fa'];
  
?>
  
" " method="POST">
temperature:
"text" name="fa" value="" />;

"submit" name="Convert to Celsius" />
    if (!is_null($f)){
        $c = ($f-32)*5/9;
        printf("%.2lf is %.2lfC",$f,$c);
    }
?>
 
  
 
都是一些简单的表单处理~

热门栏目