我正在尝试通过填写并发送表单来发送电子邮件。
用户可以填写表格并根据选项中的值收到结果,用户填写的信息将通过电子邮件发送到电子邮件帐户。
当发送电子邮件时,它仅发送组内的第一个选项,例如在问题1中,如果单击该组中的选项3,它仍将发送,就像发送了选项一一样。
任何反馈都会非常感激,只是完全被这个反击......
的index.php
<div class="form-style" id="contact_form">
<div class="form-style-heading">Contact Us</div>
<div id="contact_results"></div>
<div id="quiz_result"></div>
<div id="contact_body">
<div class="page">
<label>
<span>Name <span class="required">*</span></span>
<input type="text" name="name" id="name" required="true" class="input-field"/>
</label>
<label>
<span>Email <span class="required">*</span></span>
<input type="email" name="email" required="true" class="input-field"/>
</label>
<label>
<span>Company <span class="required">*</span></span>
<input type="text" name="company" required="true" class="input-field"/>
</label>
<label>
<span>Job Title <span class="required">*</span></span>
<input type="text" name="jobtitle" required="true" class="input-field"/>
</label>
</div>
<div class="page">
<legend>1. Question Number 1?</legend>
<input type="radio" name="question1" class="option" value="100"><label>Answer One</label><br />
<input type="radio" name="question1" class="option" value="75"><label>Answer Two</label><br />
<input type="radio" name="question1" class="option" value="50"><label>Answer Three</label><br />
<input type="radio" name="question1" class="option" value="0"><label>Answer Four</label>
</div>
<div class="page">
<legend>2. Question Number 2?</legend>
<input type="radio" name="question2" class="option" value="100"><label>Answer One</label><br />
<input type="radio" name="question2" class="option" value="75"><label>Answer Two</label><br />
<input type="radio" name="question2" class="option" value="50"><label>Answer Three</label><br />
<input type="radio" name="question2" class="option" value="0"><label>Answer Four</label>
</div>
<div class="page">
<legend>3. Question Number 3?</legend>
<input type="radio" name="question3" class="option" value="20"><label>Answer One</label><br />
<input type="radio" name="question3" class="option" value="40"><label>Answer Two</label><br />
<input type="radio" name="question3" class="option" value="60"><label>Answer Three</label><br />
<input type="radio" name="question3" class="option" value="80"><label>Answer Four</label><br />
<input type="radio" name="question3" class="option" value="100"><label>Answer Five</label>
</div>
<div class="page">
<legend>4. Question Number 4?</legend>
<input type="radio" name="question4" class="option" value="100"><label>Yes</label><br />
<input type="radio" name="question4" class="option" value="0"><label>No</label>
</div>
<div class="page">
<legend>5. Question Number 5?</legend>
<input type="radio" name="question5" class="option" value="20"><label>Answer One</label><br />
<input type="radio" name="question5" class="option" value="40"><label>Answer Two</label><br />
<input type="radio" name="question5" class="option" value="60"><label>Answer Three</label><br />
<input type="radio" name="question5" class="option" value="80"><label>Answer Four</label><br />
<input type="radio" name="question5" class="option" value="100"><label>Answer Five</label>
</div>
<div class="page">
<legend>6. Question Number 6?</legend>
<input type="radio" name="question6" class="option" value="100"><label>Answer One</label><br />
<input type="radio" name="question6" class="option" value="80"><label>Answer Two</label><br />
<input type="radio" name="question6" class="option" value="60"><label>Answer Three</label><br />
<input type="radio" name="question6" class="option" value="40"><label>Answer Four</label><br />
<input type="radio" name="question6" class="option" value="20"><label>Answer Five</label>
</div>
<label>
<input type="button" id="next" value="Next" onlick="sum_values()" />
</label>
</div>
</div>
contact_me.php
if($_POST) {
$to_email = "receiver@email.com"; //Recipient email, Replace with own email here
$from_email = 'sender@email.com'; //from mail, it is mandatory with some hosts and without it mail might endup in spam.
//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);
$user_company = filter_var($_POST["user_company"], FILTER_SANITIZE_STRING);
$user_job_title = filter_var($_POST["user_job_title"], FILTER_SANITIZE_STRING);
$question_one = filter_var($_POST["questionOne"], FILTER_SANITIZE_STRING);
$question_two = filter_var($_POST["questionTwo"], FILTER_SANITIZE_STRING);
$question_three = filter_var($_POST["questionThree"], FILTER_SANITIZE_STRING);
$question_four = filter_var($_POST["questionFour"], FILTER_SANITIZE_STRING);
$question_five = filter_var($_POST["questionFive"], FILTER_SANITIZE_STRING);
$question_six = filter_var($_POST["questionSix"], FILTER_SANITIZE_STRING);
//additional php validation
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
//email body
$message_body = "Name: ".$user_name."\r\nEmail: ".$user_email."\r\nCompany: ".$user_company."\r\nJob Title: ".$user_job_title."\r\n\n\nQuestion 1: ".$question_one."\r\nQuestion 2: ".$question_two."\r\nQuestion 3: ".$question_three."\r\nQuestion 4: ".$question_four."\r\nQuestion 5: ".$question_five."\r\nQuestion 6: ".$question_six ;
//proceed with PHP email.
$headers = 'From: '. $from_email .'' . "\r\n" .
'Reply-To: '.$user_email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, 'Survey Results', $message_body, $headers);
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 email'));
die($output);
}
}
App.js
$(document).ready(function() {
var totalPages = $('.page').size();
var currentPage = 0;
$pages = $('.page');
$pages.hide();
$($pages.get(currentPage)).fadeIn();
$('#next').click(function () {
$($pages.get(currentPage)).fadeOut(function () {
currentPage = currentPage + 1;
if (currentPage == totalPages) {
var total = 0;
$("input[type=radio]:checked").each(function() {
total+= parseInt($(this).attr("value"));
});
var answerOne = "Result 1";
var answerTwo = "Result 2";
var answerThree = "Result 3";
var answerFour = "Result 4";
var answerFive = "Result 5";
if (total < 265) {
$("#quiz_result").append('<div>' + answerOne + '</div>');
} else {
if (total > 266 && total < 355) {
$("#quiz_result").append('<div>' + answerTwo + '</div>');
} else {
if (total > 356 && total < 475) {
$("#quiz_result").append('<div>' + answerThree + '</div>');
} else {
if (total > 476 && total < 560) {
$("#quiz_result").append('<div>' + answerFour + '</div>');
} else {
if (total > 570 && total <= 600) {
$("#quiz_result").append('<div>' + answerFive + '</div>');
} else {
$("#quiz_result").append('<div>' + answerOne + '</div>');
}
}
}
}
}
post_data = {
'user_name' : $('input[name=name]').val(),
'user_email' : $('input[name=email]').val(),
'user_company' : $('input[name=company]').val(),
'user_job_title' : $('input[name=jobtitle]').val(),
'questionOne' : $('input[name=question1]').val(),
'questionTwo' : $('input[name=question2]').val(),
'questionThree' : $('input[name=question3]').val(),
'questionFour' : $('input[name=question4]').val(),
'questionFive' : $('input[name=question5]').val(),
'questionSix' : $('input[name=question6]').val(),
};
//Ajax post data to server
$.post('contact_me.php', post_data, function(response){
if(response.type == 'error'){ //load json data from server and output message
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">'+response.text+'</div>';
//reset values in all input fields
$("#contact_form input[required=true]").val('');
$("#contact_form #contact_body").slideUp(); //hide form after success
}
$("#contact_form #contact_results").hide().html(output).slideDown();
}, 'json');
$('#next').hide();
} else {
//otherwise show the next question
$($pages.get(currentPage)).fadeIn();
}
});
});
});