清理并发送表单中的复选框结果

时间:2014-11-29 10:26:22

标签: php html email checkbox html-email

我有一个基于WordPress的网站,其中包含{&#39;页面模板&#39;上的预订<form>

由于我不熟悉PHP,我不太确定我错在哪里。

我需要在checkboxes中为网站提供的服务添加一些<form>,并使用以下文件:

<?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标记,并且在网站的前端看起来与此类似:

enter image description here


但是,它似乎不会在发送的电子邮件中包含复选框信息。这封电子邮件只是简单地读取Services Selected:而没有勾选服务的名称:

enter image description here


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

消毒:

$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

如何使用我目前拥有的checkboxes来实施,整理,发送和接收<form>的结果?

非常感谢任何帮助,
感谢。

1 个答案:

答案 0 :(得分: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