Jump to content

Recommended Posts

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>

 

only checked checkboxes are set in the submitted form data. for what you are doing, you should only be concerned if they are set or are not set, not what the value is (the default value will be the text 'on').

i recommend that make an array that defines the checkbox field names and labels, then loop over this defining array to dynamically produce the form fields, pre-checking any existing checked boxes, in the case of a user/validation error, then loop over this defining array in the post method form processing code, testing which one(s) are set or are not set in the form data to produce whatever result you want.

  • Great Answer 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.