我最初使用this tutorial构建了表单,但现在我需要添加一个用于注册简报的复选框。我已经尝试过几个已经发现的修复程序,但似乎没有一个正常工作,我对PHP,AJAX,jQuery都很陌生。它非常令人沮丧:/
这是HTML:
<div id="form-messages"></div>
<form class="green-txt" id="ajax-contact" method="post" action="mailer.php">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control email-in" id="email" name="email" placeholder="" required>
</div>
<div class="form-group form-inline">
<label for="subscribe">Subscribe: </label>
<input class="sub-btn" type="checkbox" id="subscribe" name="subscribe">
</div>
<div class="row">
<div class="col-xs-6 col-xs-offset-3 col-sm-4 col-sm-offset-8 col-md-4 col-md-offset-8">
<button type="submit" class="btn-success btn btn-submit">Send</button>
</div>
</div>
</form>
&#13;
js:
$(function() {
// Get the form.
var form = $('#ajax-contact');
// Get the messages div.
var formMessages = $('#form-messages');
// Set up an event listener for the contact form.
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var dataString = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: dataString
})
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#name').val('');
$('#email').val('');
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
});
});
&#13;
PHP:
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$subscribe = trim($_POST["subscribe"]);
// Check that data was sent to the mailer.
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "";
// Set the email subject.
$subject = "UP News New Contact - $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$subscribe = "Subscribe: $subscribe\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You!";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
&#13;
我可以在这做什么来处理复选框数据?
答案 0 :(得分:0)
是。只有在检查时才提交。
您可以更改代码
$subscribe = trim($_POST["subscribe"]);
到
$subscribe = isset($_POST["subscribe"]) ? 'Yes' : 'No' ;
修改强> 更改行$ subscribe =&#34;订阅:$ subscribe \ n&#34 ;;将是$ email_content。=&#34;订阅:$ subscribe \ n&#34;
答案 1 :(得分:0)
你需要检查$ _POST [“subscribe”]的值是否来自你的php代码。 使用isset()函数来做...
编辑你的剧本,现在应该可以正常工作。
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
//$subscribe = trim($_POST["subscribe"]);
$subscribe = (isset($_POST["subscribe"]))?"Yes":"No";
// Check that data was sent to the mailer.
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "";
// Set the email subject.
$subject = "UP News New Contact - $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$subscribe = "Subscribe: $subscribe\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You!";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}