-1

I have created a message form in my website, now I am adding a text box to it. I am getting the checkbox value is ON in the email if checkbox checked blank if unchecked.

Like this enter image description here

I have to make the text Checkbox checked if Checked and Checkbox Unchecked if unchecked instead of Blank & ON Value.

Here is my Contact Form HTML

    <div class="contact-form">
     <form id="contact-form" method="post" action="php/contact-form-handler.php">
        <input name="name" type="text" class="form-control" placeholder="Your Name" required="required" />
        <br />
        <input name="email" type="email" class="form-control" placeholder="Your Email" required="required" />
        <label class="form-label opt-in-label"><input type="checkbox" class="opt-in-checkbox" name="checkbox"><span class="opt-in">Check here to receive email updates</span></label>
        <input name="subject" type="text" class="form-control" placeholder="Subject" required="required" />
   <textarea name="message" class="form-control" placeholder="Message" row="10" required="required" /></textarea>
            <br />
            <input type="submit" class="from-control submit" value="SEND MESSAGE" />
  </form>
    </div>

Here is the PHP code

<?php
 $name = $_POST['name'];
 $visitor_email = $_POST['email'];
 $subject = $_POST['subject'];
 $message = $_POST['message'];
 $checkbox = $_POST['checkbox'];

 $email_from = 'xxx@gmail.com';
 
 $email_subject = "New Form Submission";
 
 $email_body = "User Name: $name\n".
     "User Email: $visitor_email\n".
      "Checkbox: $checkbox\n".
          "Subject: $subject\n".
           "User Message: $message\n";
      
 $to = "yyy@gmail.com";
 
 $headers = "From: $email_from \r\n";
 
 $headers .= "Reply-To: $visitor_email \r\n";
 
 mail($to,$email_subject,$email_body,$headers);
 
 header("Location: /contact-us.html");
?>
Doodu
  • 107
  • 5

2 Answers2

1

Try

$checkbox = isset($_POST['checkbox']) ? "checked" : "unchecked";
santo
  • 418
  • 1
  • 3
  • 13
-1

You just forgotte to add condition.

Here is latest code please check carefully and then you can implement in your current code.

if(!empty($_POST)) {
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $checkbox = 'unchecked';
    if( isset($_POST['checkbox']) && $_POST['checkbox'] == true){
       $checkbox = 'checked';
    }

    $email_from = 'xxx@gmail.com';
    $email_subject = "New Form Submission";
    $email_body = "User Name: $name\n".
                    "User Email: $visitor_email\n".
                        "Checkbox: $checkbox\n".
                            "Subject: $subject\n".
                                "User Message: $message\n";
    $to = "yyy@gmail.com";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    mail($to,$email_subject,$email_body,$headers);
    header("Location: /contact-us.html");
}
kuldip Makadiya
  • 706
  • 4
  • 23
  • `if(!empty($_POST))` < that condition will always be met (true) if the submit button is hit without entering any values in the inputs. – Funk Forty Niner Mar 14 '20 at 18:49
  • @Funk Forty Ninner, But have you checked user code they just add without check that data is empty or not. Also i have checked checkbox is checked or not. – kuldip Makadiya Mar 18 '20 at 13:55