【问题标题】:Sanitize and send checkbox results in a form清理并发送表单中的复选框结果
【发布时间】:2015-01-27 20:29:34
【问题描述】:

我有一个基于 WordPress 的网站,其中包含“页面模板”上的预订 <form>

由于我对 PHP 不熟悉,所以我不太确定自己哪里出错了。

我需要在<form> 中包含一些checkboxes 用于网站提供的服务,并且需要使用以下文件:

<?php
/*
 * Template Name: Booking Page
 */
?>

<?php
    // Sanitize data, or initialize if they don't exist.
    $clientname = isset($_POST['ci_name']) ? esc_html(trim($_POST['ci_name'])) : '';
    $email = isset($_POST['ci_email']) ? esc_html(trim($_POST['ci_email'])) : '';
            $services = isset($_POST['services']) ? esc_html(trim(implode(",", $_POST['services']))) : ''; // My Edit
    $message = isset($_POST['ci_comments']) ? sanitize_text_field(stripslashes($_POST['ci_comments'])) : '';

    $errorString = '';
    $emailSent = false;

    if(isset($_POST['send_booking']))
    {
        // We are here because the form was submitted. Let's validate!

        if(empty($clientname) or mb_strlen($clientname) < 2)
            $errorString .= '<li><i class="fa fa-times"></i> '.__('Your name is required.', 'ci_theme').'</li>';

        if(empty($email) or !is_email($email))
            $errorString .= '<li><i class="fa fa-times"></i> '.__('A valid email is required.', 'ci_theme').'</li>';

                // Services is optional, so, no check.  // My Edit
        // Message is optional, so, no check.    

        // Alright, lets send the email already!
        if(empty($errorString))
        {
            $mailbody  = __("Name:", 'ci_theme') . " " . $clientname . "\n";
            $mailbody .= __("Email:", 'ci_theme') . " " . $email . "\n";
                    $mailbody .= __("Services Selected:", 'ci_theme') . " " . $services . "\n"; // My Edit
            $mailbody .= __("Message:", 'ci_theme') . " " . $message . "\n";

            // If you want to receive the email using the address of the sender, comment the next $emailSent = ... line
            // and uncomment the one after it.
            // Keep in mind the following comment from the wp_mail() function source:
            /* If we don't have an email from the input headers default to wordpress@$sitename
            * Some hosts will block outgoing mail from this address if it doesn't exist but
            * there's no easy alternative. Defaulting to admin_email might appear to be another
            * option but some hosts may refuse to relay mail from an unknown domain. See
            * http://trac.wordpress.org/ticket/5007.
            */
            $emailSent = wp_mail(ci_setting('booking_form_email'), get_option('blogname').' - '. __('Booking form', 'ci_theme'), $mailbody);
            //$emailSent = wp_mail(ci_setting('contact_form_email'), get_option('blogname').' - '. __('Contact form', 'ci_theme'), $mailbody, 'From: "'.$clientname.'" <'.$email.'>');
        }

    }
?>

<?php get_header(); ?>

    <main id="main">
        <div class="container">
            <div class="row">
                <div class="col-lg-10 col-lg-offset-1">
                    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <h2 class="page-title"><?php the_title(); ?></h2>

                    <div class="row">
                        <div class="col-sm-8">
                            <article <?php post_class('entry'); ?>>
                                <?php if(!empty($errorString)): ?>
                                    <ul id="formerrors">
                                        <?php echo $errorString; ?>
                                    </ul>
                                <?php endif; ?>

                                <?php if($emailSent===true): ?>
                                    <p id="formsuccess"><i class="fa fa-check"></i> <?php _e('Your booking request has been sent. We will contact you as soon as possible.', 'ci_theme'); ?></p>
                                <?php elseif($emailSent===false and isset($_POST['send_booking']) and $errorString==''): ?>
                                    <p id="sendfail"><?php _e('There was a problem while sending the email. Please try again later.', 'ci_theme'); ?></p>
                                <?php endif; ?>


                                <?php the_content(); ?>

                                <?php if(  !isset($_POST['send_booking'])  or  (isset($_POST['send_booking']) and !empty($errorString))  ): ?>

                                <form class="booking" action="<?php the_permalink(); ?>" method="post">

                                    <div class="row">
                                        <div class="col-md-6">
                                            <input type="text" name="ci_name" id="ci_name" placeholder="<?php _e('your name', 'ci_theme'); ?>" value="<?php echo esc_attr($clientname); ?>">
                                        </div>

                                        <div class="col-md-6">
                                            <input type="email" name="ci_email" id="ci_email" class="datepicker" placeholder="<?php _e('Your Email', 'ci_theme'); ?>" value="<?php echo esc_attr($email); ?>">
                                        </div>
                                    </div>


                                    <!-- My Edits -->
                                    <div class="row">
                                        <div class="col-md-12">
                                            <hr />
                                            <p>Please tick any of the following services if you would like to include them in your package:</p>
                                        </div>

                                        <div class="col-md-6">
                                            <p><input type="checkbox" name="services[]" <?php checked($services, 'Reiki'); ?> value="Reiki"> Reiki</p>
                                            <p><input type="checkbox" name="services[]" <?php checked($services, 'Private Personal Training'); ?> value="Private Personal Training"> Private Personal Training</p>
                                            <p><input type="checkbox" name="services[]" <?php checked($services, 'Walking'); ?> value="Walking"> Walking</p>
                                            <p><input type="checkbox" name="services[]" <?php checked($services, 'Boot Camp'); ?> value="Boot Camp"> Boot Camp</p>
                                        </div>

                                        <div class="col-md-6">
                                            <p><input type="checkbox" name="services[]" <?php checked($services, 'Relaxation Massage'); ?> value="Relaxation Massage"> Relaxation Massage</p>
                                            <p><input type="checkbox" name="services[]" <?php checked($services, 'Meditation Circle'); ?> value="Meditation Circle"> Meditation Circle</p>
                                            <p><input type="checkbox" name="services[]" <?php checked($services, 'Colour Workshop'); ?> value="Colour Workshop"> Colour Workshop</p>
                                        </div>
                                    </div>
                                    <!-- End My Edits -->

                                    <div class="row">
                                        <div class="col-md-12">
                                            <textarea name="ci_comments" id="ci_comments" cols="30" rows="10" placeholder="<?php _e('Message', 'ci_theme'); ?>"></textarea>

                                            <button type="submit" name="send_booking"><?php _e('Submit', 'ci_theme'); ?></button>
                                        </div>
                                    </div>
                                </form>
                                <?php endif; ?>
                            </article>
                        </div>
                        <?php endwhile; endif; ?>
                        <?php get_sidebar(); ?>
                    </div>
                </div>
            </div>
        </div>
    </main>

<?php get_footer(); ?>

在上面的代码中,我已经为复选框放置了 HTML 标记,它在网站的前端出现了类似的内容:


但是,它似乎没有在发送的电子邮件中包含复选框信息。用于此的电子邮件仅显示Services Selected:,没有勾选服务名称:


我可以在上面的代码中找到的编辑是:

消毒:

$services = isset($_POST['services']) ? esc_html(trim(implode(",", $_POST['services']))) : '';

要发送的消息:

$mailbody .= __("Services Selected:", 'ci_theme') . " " . $services . "\n";

复选框:

<input type="checkbox" name="services[]" <?php checked($services, 'Relaxation Massage'); ?> value="Relaxation Massage"> Relaxation Massage

如何使用我目前拥有的&lt;form&gt; 实施、清理、发送和接收checkboxes 的结果?

任何帮助将不胜感激,
谢谢。

【问题讨论】:

    标签: php html email checkbox html-email


    【解决方案1】:

    您没有在 $service 变量中存储任何值。

    只需输入此代码

        $clientname = isset($_POST['ci_name']) ? esc_html(trim($_POST['ci_name'])) : '';
        $email = isset($_POST['ci_email']) ? esc_html(trim($_POST['ci_email'])) : '';
        $services = isset($_POST['services']) ? esc_html(trim(implode(",", $_POST['services']))) : ''; // My Edit
        $message = isset($_POST['ci_comments']) ? sanitize_text_field(stripslashes($_POST['ci_comments'])) : '';
    
        $errorString = '';
        $emailSent = false;
    
        if(isset($_POST['send_booking']))
        {
             print_r($_POST);exit; // You can see here your post variable value in array.
        } 
    

    代替

    $services = isset($_POST['services']); // My Edit    
    

    阅读有关三元运算符的更多信息:http://php.net/ma...operators.comparison.php

    【讨论】:

    • 您是否尝试打印您的$_POST 变量?并检查输出是什么。
    • 查看我的编辑以打印您的 post 变量。让我知道你的 post 变量的输出。
    • 您是否选中了您的复选框?因为您的复选框值未显示在您的帖子数组中。
    • 刚刚从所有复选框代码中删除了&lt;?php checked($services, 'Reiki'); ?&gt;,看看会发生什么。
    • 我在本地服务器上进行了测试,它对我来说工作正常。这是我的帖子数组Array ( [ci_name] =&gt; test [ci_email] =&gt; test@test.com [services] =&gt; Array ( [0] =&gt; Reiki [1] =&gt; Private Personal Training [2] =&gt; Walking ) [ci_comments] =&gt; test [send_booking] =&gt; )
    猜你喜欢
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2016-03-02
    • 2017-03-19
    • 1970-01-01
    • 2012-02-08
    相关资源
    最近更新 更多