天天看點

PHP函數---$_Get()和$_Post()的用法

一、$_Get()和$_Post()函數是用來傳值的,即對應兩種送出表單的方法,get和post。

二、$_Get方法

(1)擷取通過URL的傳值

Example 1

建立兩個PHP檔案,1.php,2.php

1.php代碼:

<a  herf="2.php?id='10'&name='xiaoqiang'">傳值</a>  在1.php中單擊超連結傳值      

2.2.php代碼:

PHP函數---$_Get()和$_Post()的用法
<?php

echo "$_Get['id']";

echo "$_Post['name']";

?>      
PHP函數---$_Get()和$_Post()的用法

(2)$_Get方法動态傳值

<a href="newfile.php?page=<?php echo "java";?>&pageone=<?php echo "1";?>">GET方法動态傳值</a>      
PHP函數---$_Get()和$_Post()的用法
<html>

<head>

</head>

<body>

<form action="" method="post">

<input type="text" name="test" />

<?php>

echo "$_Post['test']";  //擷取文本框中的值,并且在目前頁中顯示

?>

</body>

</html>      
PHP函數---$_Get()和$_Post()的用法
PHP函數---$_Get()和$_Post()的用法
<html>
<head>
</head>
<body>
<form action="" method="post">  //action為空表示,在目前頁面處理
<input type="checkbox" name="sports[]" value="籃球">籃球
<input type="checkbox" name="sports[]" value="足球">足球
<input type="checkbox" name="sports[]" value="乒乓球">兵乓球
<input type="submit" name="sumbit1" value="送出">
</form>
<?php 
if(isset($_POST['sumbit1'])) //issset()函數的作用是:判斷送出按鈕是否單擊,即是否已送出
{
    echo "<br />\n";
    echo "你選擇的運動是:<br />\n";
    foreach ($_POST['sports'] as $sports)//
    {
        echo "$sports";
        echo "<br />\n";
    }    
}
?>
</body>
</html>      
PHP函數---$_Get()和$_Post()的用法