简体   繁体   中英

Simple form php with checkbox

Simple mail with checkbox newsletter - what i do wrong? Everything is ok - send information exept checkbox Please help

<?php
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['text']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {


  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }
            $message .= "<strong>Client:</strong>" . strip_tags($_POST['name']) . ;
            $message .= "<strong>E-mail:</strong>" . strip_tags($_POST['email']) . ;
            $message .= "Newsletter" . strip_tags($_POST['newsletter']) ; 

            $to = 'aaa@gmailcom';   
            $subject = $_POST['subject'];
            $headers = "From: " . $_POST['email'] . "\r\n";
            $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=utf-8\r\n";

            mail($to, $subject, $message, $headers);
}
?>

And html

 <label for="newsletter">
         <input type="checkbox"  id="newsletter" name="newsletter" value="Yes" checked> Subscribe
  </label>

$_POST['newsletter'] will only exist if checkbox is selected.

You can try something like this

<?php
$info = "";
if(isset($_POST['newsletter'])){
    $info = "You want to receive the newsletter";
}
else{ $info = "You dont want the newsletter"; }

$message .= "Newsletter :" . $info ; 
?>

Hope this help ps: Use var_dump($_POST) to debug your posted datas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM