好的,我有一个contact.php表单,我过去曾使用过,但出于某种原因,我不会使用我正在处理的新模板。我希望有人可以帮助指出我没有看到的东西。模板本身正在使用bootstrap。请问,但它让我疯狂lol
HTML表格
<form class="form" method="post" action="contact.php" role="form">
<div class="form__inner">
<div class="form__group form__column">
<input type="text" class="form__element field" name="username" placeholder="Your name" pattern="^\S[а-яА-ЯёЁa-zA-Z\s]+\S" required="required" data-error="Please enter your name"> <i class="icon form__icon icon-user"></i> <span class="form__decor-line"></span></br><div class="help-block with-errors"></div>
</div>
<div class="form__group form__column">
<input type="email" class="form__element field" name="usermail" placeholder="Your email" required="required" data-error="Please enter a valid e-mail"> <i class="icon form__icon icon-envelope"></i> <span class="form__decor-line"></span></br><div class="help-block with-errors"></div>
</div>
</div>
<div class="form__group">
<select class="form__element select" name="subject">
<option class="select__option">Subject</option>
<option class="select__option">Job</option>
<option class="select__option">Digital Marketing Workshop</option>
<option class="select__option">Testimonial</option>
<option class="select__option">Other</option>
</select><i class="icon form__icon icon-book"></i></div>
<div class="form__group">
<textarea class="form__element field textarea" name="msg" placeholder="Your message" required="required" data-error="Please leave a message"></textarea><i class="icon form__icon icon-bubbles"></i> <span class="form__decor-line"></span></br><div class="help-block with-errors"></div></div>
<div class="form__button-box">
<button class="button">Send</button>
</div>
</form>
Contact.php
<?php
// configure
$from = 'Support <test@test.com>';
$sendTo = 'Sales <test@test.com>';
$subject = 'I would like to arrange a FREE quotation';
$fields = array('username' => 'username', 'usermail' => 'usermail' , 'subject' => 'subject' , 'msg' => 'msg'); // array variable name => Text to appear in email
$okMessage = 'Contact form successfully submitted. Thank you, we will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
?>
提前感谢您对这些家伙的帮助,与流感atm斗争,目前有点离开它大声笑
答案 0 :(得分:0)
您的表单正在工作中看到图片,只需要修改一些内容,首先您的按钮必须具有名称属性然后在您的控制器上检查在发送表单之前是否设置了按钮,可能会发生您发送的空表单。
page.php文件
<form class="form" method="post" action="contact.php" role="form">
<div class="form__inner">
<div class="form__group form__column">
<input type="text" class="form__element field" name="username" placeholder="Your name" pattern="^\S[а-яА-ЯёЁa-zA-Z\s]+\S" required="required" data-error="Please enter your name"> <i class="icon form__icon icon-user"></i> <span class="form__decor-line"></span></br><div class="help-block with-errors"></div>
</div>
<div class="form__group form__column">
<input type="email" class="form__element field" name="usermail" placeholder="Your email" required="required" data-error="Please enter a valid e-mail"> <i class="icon form__icon icon-envelope"></i> <span class="form__decor-line"></span></br><div class="help-block with-errors"></div>
</div>
</div>
<div class="form__group">
<select class="form__element select" name="subject">
<option class="select__option">Subject</option>
<option class="select__option">Job</option>
<option class="select__option">Digital Marketing Workshop</option>
<option class="select__option">Testimonial</option>
<option class="select__option">Other</option>
</select><i class="icon form__icon icon-book"></i></div>
<div class="form__group">
<textarea class="form__element field textarea" name="msg" placeholder="Your message" required="required" data-error="Please leave a message"></textarea><i class="icon form__icon icon-bubbles"></i> <span class="form__decor-line"></span></br><div class="help-block with-errors"></div></div>
<div class="form__button-box">
<button class="button" name="send">Send</button>
</div>
</form>
contact.php
<?php
// configure
$from = 'Support <test@test.com>';
$sendTo = 'Sales <test@test.com>';
$subject = 'I would like to arrange a FREE quotation';
$fields = array('username' => 'username', 'usermail' => 'usermail' , 'subject' => 'subject' , 'msg' => 'msg'); // array variable name => Text to appear in email
$okMessage = '';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending
if(isset($_POST['send'])){
try
{
$emailText = "You have new message from contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$okMessage="Contact form successfully submitted. Thank you, we will get back to you soon!";
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
}
?>
<强>结果:强>
] 1
注意:您还需要验证您的fieds man,而不是html5 buildin必填字段使用您自己的验证,html5没有正确验证电子邮件。