2010-12-13 42 views
4

假設有一個HTML文件,其中有一個表單,其中包含一些數據,這些數據已經從用戶使用textarea和複選框輸入。如何將這些數據發送到PHP文件?如何使用PHP從HTML表單收集數據?

+1

http://www.php.net/manual/en/language.variables.external.php,http://w3schools.com/ PHP/php_forms.asp – deceze 2010-12-13 06:36:41

回答

2

所有形式的變量將是$ _GET或$ _POST陣列在PHP(取決於你使用的提交形式方法

textarea的或複選框需要被命名爲這樣的:

<!-- HTML form --> 

<form method="post" action="collect.php"> 
Comments: <textarea name="comments" cols="20" rows="5"></textarea> <br/> 
Tick to select <input type="checkbox" name="checker"/> 
</form> 



// collect.php  
$comments=""; if(isset($_POST["comments"])) { $comments = $_POST["comments"]; } 
$checker=""; if(isset($_POST["checker"])) { $comments = $_POST["checker"]; } 
1

表單中的值將在$ _GET和$ _POST數組中使用,具體取決於表單中使用的方法。

2

您可以通過提交表單然後在php文件中發佈此數據使用$_POST['fieldname'];來使用值你有HTML頁面。

0

表單的action屬性定義了表單數據發送到的php腳本(或其他腳本)。您將能夠使用$_GET和$ _ POST獲取數據。在以下示例中,文本輸入將提交到form_action.php

<form action="form_action.php" method="get"> 
    First name: <input type="text" name="fname" /><br /> 
    Last name: <input type="text" name="lname" /><br /> 
    <input type="submit" value="Submit" /> 
</form>