我应该在哪里为PHP表单添加PHP代码?

时间:2013-12-12 02:57:16

标签: php html

我似乎无法找到这个特定问题的答案,所以如果它已经得到回答并且我还没有看到它,我会道歉。

我有一个HTML表单,其中包含访问者姓名,电子邮件和消息;当然是用CSS设计的。 我准备好了PHP代码,但我不确定在哪里放置它。目前我在它自己的页面上有它,而html用“action”指向它,但是当我测试页面并提交表单时,它只是转到一个空白页面。以下是我的HTML代码示例..和PHP代码...此页面可以在这里看到... http://wayhigh.we.bs/contact.html

<form method="post" action="index.php" class="form" id="form1">

  <p class="name">
    <input name="name" type="text"            class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name"  id="name" />
  </p>

  <p class="email">
    <input name="email" type="text" class="validate[required,custom[email]] feedback- input" id="email" placeholder="Email" />
  </p>

  <p class="text">
    <textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
  </p>


  <div class="submit">
    <input type="submit" value="SEND" id="button-blue"/>
    <div class="ease"></div>
  </div>
</form>

和PHP ...

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['text'];
$from = 'From: J. Michaels'; 
$to = 'mailto:benjamminredden@gmail.com'; 
$subject = 'Hello from a visitor';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
/* Anything that goes in here is only performed if the form is submitted */
}

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>

2 个答案:

答案 0 :(得分:1)

您需要将name="submit"添加到提交按钮:

<input type="submit" name="submit" value="SEND" id="button-blue"/>

否则,if ($_POST['submit'])将无法成功。

答案 1 :(得分:0)

请注意,我在isset

中添加了if (isset($_POST['submit'])) HTML表单之前的

PHP 代码

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['text'];
$from = 'From: J. Michaels'; 
$to = 'mailto:benjamminredden@gmail.com'; 
$subject = 'Hello from a visitor';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if (isset($_POST['submit'])) {
/* Anything that goes in here is only performed if the form is submitted */
}

if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>

<form method="post" action="index.php" class="form" id="form1">

  <p class="name">
    <input name="name" type="text"            class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name"  id="name" />
  </p>

  <p class="email">
    <input name="email" type="text" class="validate[required,custom[email]] feedback- input" id="email" placeholder="Email" />
  </p>

  <p class="text">
    <textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
  </p>


  <div class="submit">
    <input type="submit" value="SEND" id="button-blue"  name="submit" />
    <div class="ease"></div>
  </div>
</form>
相关问题