0

I have a working form but I would like to add a check box to it. Sorry, completely new to PHP so I'm unsure what I would need to add to the PHP to make it work.

The HTML:

<label for="Name"></label>
<input type="text" placeholder="Name" name="Name" id="Name" />

<label for="Tel"></label>
<input type="text" placeholder="Tel" name="Tel" id="Tel" />

<label for="Email"></label>
<input type="text" name="Email" placeholder="Email address" id="Email" />

<label for="Message"></label><br />
<textarea name="Message" placeholder="Your Message" rows="10" cols="20" id="Message"></textarea>

<label for="Subscribe"></label>
<input type="checkBox" name="CheckBox" id="checkbox" /> <p>Please tick this box to be kept up to date with offers</p>

The PHP:

$EmailFrom = "me@test.com";
    $EmailTo = "me@test.com";
    $Subject = "Enquiry from website";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Tel = Trim(stripslashes($_POST['Tel'])); 
    $Email = Trim(stripslashes($_POST['Email'])); 
    $Message = Trim(stripslashes($_POST['Message'])); 
    $Message = Trim(stripslashes($_POST['Checkbox'])); 

    // validation
    $validationOK=true;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Tel: ";
    $Body .= $Tel;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $Message;
    $Body .= "\n";

    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=https://myurl.com\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }

Any help would be really appreciated. Thanks

Benny
  • 1
  • 1

1 Answers1

0

You're overwriting the $Message var with the value of the checkbox:

$Message = Trim(stripslashes($_POST['Message'])); 
$Message = Trim(stripslashes($_POST['Checkbox']));

You could also consider that checkboxes send on/off values unless you specify a different value with the parameter value="xxxx"

LordNeo
  • 1,195
  • 1
  • 8
  • 21