2010-12-13 46 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>