如何在此PHP表单中添加复选框

时间:2017-10-05 15:21:28

标签: php forms

我有一个工作表单,但我想添加一个复选框。对不起,对PHP来说是全新的,所以我不确定我需要添加到PHP中才能使它工作。

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>

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\">";
    }

任何帮助都会非常感激。感谢

1 个答案:

答案 0 :(得分:0)

您使用复选框的值覆盖$ Message var:

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

您还可以考虑复选框发送开/关值,除非您使用参数value =“xxxx”指定其他值