Checkbox value php email

I need some help with getting checkbox values sent to email using phpmailer

I have not done it before so unsure how to do it, the current code I have is below

             $postData = $_POST;
             $oneway = $_POST['oneway'];
             $return = $_POST['return'];

             $htmlContent = '<h2>Contact Form Details</h2>
            <p><b>One Way:</b> ' . $oneway . '</p>
            <p><b>Return:</b> ' . $return . '</p>
                        <form action="#errors" method="post" class="formontop">
                        <div class="booking-form">
                       <div class="row mb-4">
                       <div class="col-lg-6">
                       <div class="form-check">
                        <input type="checkbox" name="oneway" class="form-check-input" value="<?php echo !empty($postData['oneway'])?$postData['oneway']:''; ?>">
                         <label class="form-check-label" for="oneway">
                          One Way
                           </label>
                           </div>
                           </div>
                           <div class="col-lg-6">
                           <div class="form-check">
                           <input type="checkbox" name="return" class="form-check-input" value="<?php echo !empty($postData['return'])?$postData['return']:''; ?>">
                            <label class="form-check-label" for="return">
                             Return
                             </label>
                             </div>
                             </div>
                            <button class="default-btn" type="submit" name="submit">Get My Quote</button>
                            </div>
                            </div>
                            </form>
1 Like

What problem are you having? Does your code not do what you want it to? If not, please describe how the result isn’t what you want, and let us know what your desired result is, so someone can figure out why it’s not working how you want it to.

I don’t see any of the PHPMailer code in there, so is the question really “how do I send this information by email?” If so, there are many examples on the PHPMailer download site, which of those have you tried and why aren’t they suitable for your particular case?

My first step would be to create a short test script that uses PHPMailer to send some fixed text, and get that working. Once you’ve done that, amend your form code so that if the form has been submitted, assemble your email using the form variables, and send it. Because you’ve already sent a fixed-text email, that step will be easy.

Checkbox values in PHP are simply a there-or-not. If the box is checked on the form, the commensurate value in the $_POST (or $_GET) array is set. If it’s not checked, it doesn’t get set, and so $_POST[‘mything’] wont exist.

You can check for these values with array_key_exists or isset, or bypass using Null Coalescence (??). You should check for (or force) their existence before trying to use them.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.