[英]PHP contact form will just refresh the page after submission.
After searching for about 3 hours i still can't figure this one out. 搜索约3个小时后,我仍然无法弄清楚这一点。 I Have a html template with a contact form and im trying to make it work using a PHP script.
我有一个带有联系表单的html模板,我试图使用PHP脚本使其工作。 I changed the template to PHP and pasted the PHP form script in it.
我将模板更改为PHP,并在其中粘贴了PHP表单脚本。 everything is working fine except the confirmation text.
除确认文字外,其他所有功能均正常运行。 After a successful submission it will just refresh the page instead of printing "Your mail has been sent successfuly ! Thank you for your feedback".
成功提交后,它只会刷新页面,而不会打印“您的邮件已成功发送!谢谢您的反馈”。 i do not want a redirect, i just want it to print on the same page.
我不希望重定向,我只希望它打印在同一页面上。
Any ideas? 有任何想法吗? I got a sample of my code.
我得到了代码示例。
<form action="<? echo $_SERVER['PHP_SELF']; ?>" id="contact-form" method="post" class="form afsana-form" role="form">
<div class="row">
<div class="col-sm-12 form-group">
<input class="form-control afsana-style" id="name" name="name" placeholder="name" type="text" required autofocus />
</div>
<div class="col-sm-12 form-group">
<input class="form-control afsana-style" id="email" name="email" placeholder="email" type="email" required />
</div>
<div class="col-sm-12 form-group">
<textarea class="form-control" id="message" name="message" placeholder="message" rows="5"></textarea>
</div>
<div class="col-sm-12 form-group">
<button class="btn btn-primary afsana-btn" name="submit" value="verzenden" type="submit">Verzenden <i class="ion-arrow-graph-up-right"></i></button>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["email"]==""||$_POST["message"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$subject = (Contact_form);
$message = $_POST['message'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("something@domain.com", $subject, $message, $headers);
echo "Your mail has been sent successfuly ! Thank you for your feedback";
}
}
}
?>
First, you have this: $subject = (Contact_form);
首先,您有:
$subject = (Contact_form);
which should throw an error, so I assume you have error reporting turned off. 这应该引发错误,因此我假设您已关闭错误报告。 When developing, you should have error reporting on so you can see errors in your code... Else you are just working blind.
在开发时,应该报告错误,以便您可以看到代码中的错误...否则,您将只是盲目工作。 I don't mean by throwing tacky
error_reporting(0)
in every file either, I mean to set your error reporting level to E_ALL
in your php.ini
. 我的意思也不是在每个文件中都添加俗气的
error_reporting(0)
,而是在php.ini
中将错误报告级别设置为E_ALL
。
You also have: $headers .= 'Cc:'. $email2 . "\\r\\n";
您还有:
$headers .= 'Cc:'. $email2 . "\\r\\n";
$headers .= 'Cc:'. $email2 . "\\r\\n";
However, $email2
is not defined anywhere, so you would get an error here too.. which is why it's important to test with error reporting on . 然而,
$email2
没有在任何地方定义,所以你会得到一个错误太..这就是为什么它有错误报告对测试很重要。
See if this works: 查看是否可行:
<?php
$error = '';
if(isset($_POST['submit']))
{
if ( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) )
{
$email = $_POST['email'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if ( $email = filter_var($email, FILTER_VALIDATE_EMAIL) )
{
$subject = '(Contact_form)';
$message = $_POST['message'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$message = wordwrap($message, 70);
if ( $result = mail("something@domain.com", $subject, $message, $headers) ) {
$error = 'Success';
} else {
$error = 'There was an error sending your email!';
}
} else {
$error = 'Invalid Email Address!';
}
} else {
$error = 'Please fill all fields.';
}
}
?>
<p><?= $error ?></p>
<form action="" method="post">
<input type="text" name="name" value="" /><br />
<input type="email" name="email" value="" /><br />
<textarea name="message" rows="5"></textarea><br />
<input type="submit" name="submit" value="submit" />
</form>
Try to put in $subject just a string value like: 尝试只将$ subject放入一个字符串值,例如:
$subject = 'Test subject';
change also the following line to this (there is no $email2 defined): 还将以下行更改为此(未定义$ email2):
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
and give it a try. 并尝试一下。 You can also put as first line of your code
您也可以将代码作为第一行
<?php error_reporting(E_ALL); ?>
and check for errors when submiting the form. 并在提交表单时检查错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.