PHP提交表单
二种提交方式,提交到其它页面和提交到本身见代码:
<!DOCTYPE html> <html> <title>表单提交开始</title>
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <body>
<a href= "../index.php" >返回首页</a>
<hr></hr> <b>一、表单提交到另一个php上</b><br /> <form action= "formTo.php" method= "POST" >
Name: <input type= "text" name= "username" ><br />
Email: <input type= "text" name= "email" ><br />
<input type= "submit" name= "submit" value= "提交" /> </form> <hr/> <b>二、将一个表单 POST 给自己并在提交时显示数据:</b><br /> <br/> <?php if (isset( $_POST [ 'action' ]) && $_POST [ 'action' ] == 'submitted' ) {
echo '<pre>' ;
print_r( $_POST );
echo '<a href="' . $_SERVER [ 'PHP_SELF' ] . '">返回重填</a>' ;
echo '</pre>' ; } else { ?> <form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method= "post" >
Name: <input type= "text" name= "personal[name]" ><br />
Email: <input type= "text" name= "personal[email]" ><br />
Beer: <br>
<select multiple name= "beer[]" >
<option value= "warthog" >Warthog</option>
<option value= "guinness" >Guinness</option>
<option value= "stuttgarter" >Stuttgarter Schwabenbr</option>
</select><br />
<input type= "hidden" name= "action" value= "submitted" />
<input type= "submit" name= "submit" value= "submit me!" /> </form> <?php } ?> </body> </html> |
formTo.php
<!DOCTYPE html> <html> <title>表单提交</title>
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <body>
<a href= "../index.php" >返回首页</a>
<hr></hr> <?php
// 自 PHP 4.1.0 起可用
echo "<br/> _POST" ; echo $_POST [ 'username' ];
echo "<br/> _REQUEST" ; echo $_REQUEST [ 'email' ];
import_request_variables( 'p' , 'p_' );
echo "<br/> p_username" ; echo $p_username ;
// PHP 6以后将无效。自 PHP 5.0.0 起,这些较长的预定义变量
// 可用 register_long_arrays 指令关闭。
echo "<br/> HTTP_POST_VARS" ; echo $HTTP_POST_VARS [ 'username' ]; ?> </body> </html> |