让我说我有一个html表格如下:
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname"><br><br>
<input type="submit" value="Submit">
</form>
我有一个PHP函数
<?php
function show()
{
$firstName= (What i submitted on form)
$lastName= (What i submitted on form)
echo "First Name: $firstName<br>";
echo "Last Name: $lastName<br>";
}
?>
我怎样才能这样做,当我按提交时它将提交给php函数show()
的信息传递给它并运行它?
答案 0 :(得分:3)
您需要使用表单上的method
和action
属性,即:
<form method="post" action="form.php">
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname"><br><br>
<input type="submit" value="Submit">
</form>
然后在PHP
上使用$_POST
来检索值,即:
<强> form.php的强>
<?php
if(!empty($_POST['firstname']) and !empty($_POST['lastName'])){
$firstName= $_POST['firstname'];
$lastName= $_POST['lastName'];
echo "First Name: $firstName<br>";
echo "Last Name: $lastName<br>";
}
?>