我正在尝试仅使用PHP验证我的RSVP表单。当表单不完整时,用户应收到错误消息。我试图避免使用jQuery。
我正在使用本教程: http://premium.wpmudev.org/blog/how-to-build-your-own-wordpress-contact-form-and-why/
表单运行正常,但我还没有能够显示错误消息。我正在使用Wordpress,我希望表单出现在每个页面的页脚;不确定这是否使问题复杂化。这是我的代码:
<?php
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message) {
global $response;
if ($type == "success") {
$response = "<div class='success'>{$message}</div>";
} else {
$response = "<div class='error'>{$message}</div>";
}
}
//response messages
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//variables defined for messages
$email = $_POST["rsvp_email"];
$name = $_POST["rsvp_name"];
$attend = $_POST["rsvp_attend"];
$number = $_POST["rsvp_number"];
//variables defined for message to admin
$to = get_option('admin_email'); //sending to wordpress admin email
$subject = "Just Kidding You Foo";
$headers = "From: $email\n";
$message = "$name $attend.\n RSVPs $number of people";
//conditional statements used for form validation
//validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
my_contact_form_generate_response("error", $email_invalid);
} else { //email is valid
//validate presence of name and message
if(empty($name) || empty($attend) || empty($number)) {
my_contact_form_generate_response("error", $missing_content);
} else { //ready to go!
$sent = wp_mail($to,$subject,$message,$headers);
if($sent) {
my_contact_form_generate_response("success", $message_sent); //message sent!
} else {
my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
?>
<div id="page-rsvp">
<h1>RSVP</h1>
<div id="respond">
<?php echo $response; ?>
<form action="<?php the_permalink(); ?>" method="post">
<!--Name here-->
<div class="rsvp-full"><label for="rsvp_name"><input type="text" name="rsvp_name" value="Your name"></label></div>
<div class="rsvp-full"><label for="rsvp_email"><input type="text" name="rsvp_email" value="Your email"></label></div>
<!--status of attendance-->
<div class="rsvp-full">
<div class="rsvp-element"><input id="radio-button" type="radio" name="rsvp_attend" value="accepts">Accepts</div>
<div class="rsvp-element"><input id="radio-button" type="radio" name="rsvp_attend" value="declines">Declines</div>
</div>
<!--number of guests attending-->
<div class="rsvp-full"><input type="number" name="rsvp_number" min="1" max="5">Total number of guests attending</div>
<div id="submit-button" class="rsvp-full"><input id="submit-button" type="submit"></div>
</form>
</div>
</div>
TIA !!!
答案 0 :(得分:1)
我对WP并不熟悉,但如果我理解正确,我相信你会努力确保填写所有字段。
检查您的括号! 您需要确保您的花括号在您想要的位置打开和关闭。否则,页面的输出将不会显示。我写了所有的大括号,因为我不够聪明,无法确定我知道他们在哪里开始和停止。我冒昧地将它们编辑到你的问题中。我相信最后有一个人失踪了。
一旦我修复了括号并删除了我的电脑没有的功能,它就能正常工作。
提示0:尝试在此脚本的顶部为此脚本启用错误报告 - error_reporting(E_ALL);
。我总是为发展而努力。
提示1:使用placeholder
属性代替value
来代替“你的名字”。
提示2:确保已设置$_POST
个变量。我会通过检查它们是否已设置然后将它们设置为''
(如果它们不是)来执行此操作;像这样的东西:
//variables defined for messages
// you could do it like this:
if (isset($_POST["rsvp_email"])) {
$email = $_POST["rsvp_email"];
} else {
$email = '';
}
// or like this:
$name = '';
if (isset($_POST["rsvp_name"])) {
$name = $_POST["rsvp_name"];
}
// or even using a ternary operator:
$attend = isset($_POST["rsvp_attend"]) ? $_POST["rsvp_attend"] : '';
//but this will trigger a "Notice" error if the post var isn't set.
$number = $_POST["rsvp_number"];