【问题标题】:Validating simple RSVP form through PHP通过 PHP 验证简单的 RSVP 表单
【发布时间】:2015-02-26 00:55:19
【问题描述】:

我正在尝试仅使用 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!!!

【问题讨论】:

    标签: php wordpress forms


    【解决方案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"];
    

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 2014-02-19
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 2011-05-18
      • 2010-10-18
      • 1970-01-01
      相关资源
      最近更新 更多