简单的PHP表单

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (43)

我有一个简单的联系,我希望通过一个功能“POST”,但我似乎无法让它工作?

当我将动作发送到外部“send.php”文件但是我希望将所有内容保存在一个文件中时,该表单有效。

<form action="send()" id="contactform" method="POST">
<p>Name<input type="text" name="name">
  Email
  <input type="text" name="email">
  Phone
  <input type="text" name="phone">
</p>
<p>Message
  <textarea name="message" rows="6" cols="25"></textarea>
  <br />
<input type="submit" value="Send">
</p>
</form>

<?php
function send()
{
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n  Phone: $phone \n Email: $email \n Message: $message";
$recipient = "email@email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>
提问于
用户回答回答于

You can't call php code from a <form> tag. Typically you handle this as a "postback" back to the same page, and check if a form variable is set to determine if the postback has happened:

<form action="" id="contactform" method="POST">

Then in your code:

if (isset($_POST['name']))
    send();
用户回答回答于

form标签更改为

<form action="" ... >
    ...
</form>

send()如果$_POST['message']设置,则在声明后调用。

action属性只告诉浏览器它应该向哪个网页发送信息; 它没有任何关系,告诉它一旦它到达那个运行的功能。

扫码关注云+社区

领取腾讯云代金券