Поэтому я пытаюсь отправить значения из моей контактной формы (contact.php) в мой файл send_form_email.php.
По какой-то причине я не получаю значения из contact.php. Буду очень признателен за вашу помощь!
Вот код формы из моего файла contact.php:
<form role="form" id="contactform" name="contactform" enctype='multipart/form-data' method="post" action="send_form_email.php">
<div class="form-group">
<label for="inputFName" style="color: #ECC444; text-shadow: 2px 2px #414141;">First Name:</label>
<input type="text" class="form-control" id="inputFName" placeholder="Enter First Name" maxlength="50" size="30">
</div>
<div class="form-group">
<label for="inputLName" style="color: #ECC444; text-shadow: 2px 2px #414141;">Last Name:</label>
<input type="text" class="form-control" id="inputLName" placeholder="Enter Last Name" maxlength="50" size="30">
</div>
<div class="form-group">
<label for="inputEmail" style="color: #ECC444; text-shadow: 2px 2px #414141;">Email Address:</label>
<input type="text" class="form-control" id="inputEmail" placeholder="Enter Email" maxlength="80" size="30">
</div>
<div class="form-group">
<label for="inputComments" style="color: #ECC444; text-shadow: 2px 2px #414141;">Comments:</label>
<textarea class="form-control"name="inputComments" rows="3" maxlength="1000" cols="25" rows="6"></textarea>
</div>
<?php
require_once('recaptchalib.php');
$publickey = "<public-key>";
echo recaptcha_get_html($publickey);
?>
<br/>
<button type="button" class="btn btn-default" onclick="sendEmail()">Submit</button>
</form>
После нажатия кнопки «Отправить» вызывается следующая функция Javascript:
<script language="javascript">
function sendEmail() {
document.forms["contactform"].submit();
}
</script>
Это отправляет форму и вызывает файл send_form_email.php:
<?php session_start();
require_once('recaptchalib.php');
$privatekey = "privatekey";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}
else {
if(isset($_POST['inputEmail'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Example Contact Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['inputFName']) ||
!isset($_POST['inputLName']) ||
!isset($_POST['inputEmail'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
date_default_timezone_set('America/Montreal');
$first_name = $_POST['inputFName']; // required
$last_name = $_POST['inputLName']; // required
$email_from = $_POST['inputEmail']; // required
$comments = $_POST['inputComments']; // required
$date = getdate();
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details submitted on ".$date." are as follows:\n\n";
$email_message_sender = "Thank you for your contact request! We will be reply as soon as possible!";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href", "./");
return str_replace($bad,"",$string);
}
function clean_link($string) {
$bad = array("./");
return str_replace($bad,"/",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$email_message_sender .= "First Name: ".clean_string($first_name)."\n";
$email_message_sender .= "Last Name: ".clean_string($last_name)."\n";
$email_message_sender .= "Email: ".clean_string($email_from)."\n";
$email_message_sender .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
$headers2 = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_from, $email_subject, $email_message_sender, $headers2);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
}
?>
Я пропустил некоторые значения, такие как открытый / закрытый ключ для recaptcha и мой адрес электронной почты по понятным причинам.
Я не могу понять, что я делаю не так.
Спасибо, парни.
Все ваши поля формы не имеют своих name
приписывать. Без этого их ценности не отправлено на сервер.
Например:
<input type="text" class="form-control" id="inputFName" placeholder="Enter First Name" maxlength="50" size="30">
пропал, отсутствует name="inputFName"
:
<input type="text" name="inputFName" class="form-control" id="inputFName" placeholder="Enter First Name" maxlength="50" size="30">
Других решений пока нет …