我似乎无法找到这个特定问题的答案,所以如果它已经得到回答并且我还没有看到它,我会道歉。
我有一个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>';
}
}
?>
答案 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>