作为一名温和的中级网络开发人员,我从未实际实施过联系表单。问题是我无法收到电子邮件。
HTML:
<form action="php/handleFormSubmit.php" id="contact-form" role="form" method="POST">
<div class="ajax-hidden">
<div class="form-group wow fadeInUp">
<label class="sr-only" for="c_name">Name</label>
<input type="text" id="c_name" class="form-control" name="c_name" placeholder="Name">
</div>
<div class="form-group wow fadeInUp" data-wow-delay=".1s">
<label class="sr-only" for="c_email">Email</label>
<input type="email" id="c_email" class="form-control" name="c_email" placeholder="E-mail">
</div>
<div class="form-group wow fadeInUp" data-wow-delay=".2s">
<textarea class="form-control" id="c_message" name="c_message" rows="7" placeholder="Message"></textarea>
</div>
<button type="submit" class="btn btn-lg btn-block wow fadeInUp" data-wow-delay=".3s">Send Message</button>
</div>
<div class="ajax-response"></div>
</form>
PHP:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['c_name'];
$visitor_email = $_POST['c_email'];
$message = $_POST['c_message'];
$email_from = "email@email.com";
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "email@email.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
}
if(isset($_POST['c_name'])){
$res['sendstatus'] = 1;
$res['message'] = 'Form Submission Successful';
echo json_encode($res);
}
?>
我知道if(isset($_POST['submit']))
在刷新/登陆时摆脱了烦人的电子邮件,但我的提交没有转到我的电子邮箱。
帮助?我很感激。
答案 0 :(得分:1)
检查第一行,
它说如果设置了$_POST['submit']
,那么执行此操作{}
。
但你的html表单没有那个字段。
在表单中添加此字段:
<input type="hidden" name="submit">
答案 1 :(得分:0)
通过Ajax发送邮件的代码
<?php
if($_POST)
{
$to_email = "er.shakun90@gmail.com"; //Recipient email, Replace with own email here
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$user_name = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
//$user_email = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
//$country_code = filter_var($_POST["country_code"], FILTER_SANITIZE_NUMBER_INT);
$phone_number = filter_var($_POST["user_phone"], FILTER_SANITIZE_NUMBER_INT);
//$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error.
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field
$output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
die($output);
}
if(strlen($message)<3){ //check emtpy message
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
$user_email = 'contact@sanktik.net';
//email body
$message_body = $message."\r\n\r\n-".$user_name."\r\nPhone Number :". $phone_number ;
$sunject = "New enquiry from wesbite";
//proceed with PHP email.
$headers = 'From: '.$user_name.'' . "\r\n" .
'Reply-To: '.$user_email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your interest.Our Team Member will get in touch with you shortly'));
die($output);
}
}
?>
答案 2 :(得分:0)
将您的按钮代码更改为:
<button type="submit" name="Submit" class="btn btn-lg btn-block wow fadeInUp" data-wow-delay=".3s">Send Message</button>
和你的php文件第一行:
if(isset($_POST['Submit']))
答案 3 :(得分:0)
不要检查POST数组中是否存在该按钮。如果您单击按钮或通过Enter press提交表单,这是有道理的!我认为通过按Enter提交,提交按钮不存在!
以下解决方案不敏感:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// sent
重要的事情:
之后,您可以执行自己的操作,例如:邮寄或保存到数据库。