php - Email contact form with response in the same page
问题描述
i've this email contact form that works well but open me the response in a blank empty page. I would the response in the same page. Can anyone help me? Thank you in advance.
<?php
// Site Info
$site_name = 'xxx';
$site_email = 'info@sitename.com';
if(isset($_POST['contact_email'])){
$contact_name = $_POST['contact_name'];
$contact_phone = $_POST['contact_phone'];
$contact_email = $_POST['contact_email'];
$contact_subject = $_POST['contact_subject'];
$contact_message = $_POST['contact_message'];
$from_email = $contact_email;
$contact_name = "Name: $contact_name <br />";
$contact_email = "Email: $contact_email <br />";
$contact_phone = "Phone Number: $contact_phone <br />";
$contact_subject = "Subject: $contact_subject <br />";
$contact_message = "Message: $contact_message <br />";
$to = $site_email;
$subject = "You have a new email from ".$site_name;
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From:'.$from_email. " \r\n";
$message = "
You have a new message! <br />
$contact_name
$contact_email
$contact_phone
$contact_subject
$contact_message
";
// Send Mail
if(@mail($to,$subject,$message,$header)) {
$send = true;
} else {
$send = false;
}
if(isset($_POST['ajax'])){
if($send)
echo 'success';
else
echo 'error';
}
}
HTML
<form id="contact_form" name="contact_form" method="post" action="page_contact_ajax.php" >
<div class="form-row field_text">
<label for="contact_name">Your Name </label>
<em>(required)</em><br>
<input id="contact_name" class="input_text required" type="text" value="" name ="contact_name">
</div>
<div class="form-row field_text">
<label for="contact_phone">Your Phone Number </label><em>(optional)</em><br>
<input id="contact_phone" class="input_text" type="text" value="" name ="contact_phone">
</div>
<div class="form-row field_text">
<label for="contact_email">Your E-Mail Address </label><em>(required)</em><br>
<input id="contact_email" class="input_text required" type="text" value="" name ="contact_email">
</div>
<div class="form-row field_text">
<label for="contact_subject">Subject </label><em>(required)</em><br>
<input id="contact_subject" class="input_text required" type="text" value="" name ="contact_subject">
</div>
<div class="form-row field_textarea">
<label for="contact_message">Message: </label><br>
<textarea id="contact_message" class="input_textarea" type="text" value="" name ="contact_message"></textarea>
</div>
<div class="form-row field_submit">
<input type="submit" value="Submit Now" id="contact_submit" class="ui button colored">
<span class="loading hide"><img src="assets/images/preloader.gif"></span>
</div>
<div class="form-row notice_bar">
<p class="notice notice_ok hide">Thank you for contacting us. We will get back to you as soon as possible.</p>
<p class="notice notice_error hide">Due to an unknown error, your form was not submitted. Please resubmit it or try later.</p>
</div>
</form>
HTML
Your Name (required)Your Phone Number (optional)
Your E-Mail Address (required)
Subject (required)
Message:
Thank you for contacting us. We will get back to you as soon as possible. Due to an unknown error, your form was not submitted. Please resubmit it or try later.
解决方案
推荐阅读
- c# - Xamarin 表单 - 根据单击的按钮更改页面内容
- reactjs - 如何在 React DraftJS 编辑器中存储 textAlignment 值?
- angular - 在 Angular 10 中使用羽毛笔编辑器自定义字体大小
- python - Django 动态设置:从管理页面访问 DEBUG
- c++ - free():使用 scanf 时指针无效
- javascript - 我可以让 React 在开发中以不同的方式编译对象扩展运算符吗?
- python - 变分自编码器的潜在维度分析:为什么使用 z_mean 而不是 z?
- python-3.x - 如何将colab中的音频文件转换为文本?
- php - 检查是否可以使用 laravel 识别路线
- gcc - gcc 10.3.1.1.fc32 构建失败并显示“gcc:致命错误:'-fuse-linker-plugin',但未找到 liblto_plugin.so”