提交表单传递给并运行PHP函数

时间:2015-10-14 23:11:46

标签: php

让我说我有一个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()的信息传递给它并运行它?

1 个答案:

答案 0 :(得分:3)

您需要使用表单上的methodaction属性,即:

<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>";
}  
?>