在php电子邮件中发送多个复选框值

时间:2019-07-06 09:04:45

标签: php phpmailer

我已经编写了一个代码,可以在数组中发送多个选定的复选框值,但是在电子邮件中却变成空白。

这是我用HTMl编写的代码:

<form class="form" action="#" method="POST">
      <?php if($msg!=''){?>
            <div class="alert alert-success">
            <?php echo $msg;?>
            </div>
        <?php } ?>
        <p class="form-p" type="Name:*"><input placeholder="Write your name here.." name="yourname" id="txtFirstName"></input></p>
        <p class="form-p" type="Mobile Number:*"><input placeholder="Let us know how to contact you back.." name="mobilenumber" onkeypress="return isNumber(event)"></input></p>
        <p class="form-p" type="Email"><input placeholder="What would you like to tell us.." name="email" id="email" type="email"></input><span class="msg error">Not a valid email address</span></p> 
        <p class="form-p" type="Delivery Address*"><input placeholder="Write your Address" name="address"></input></p>
        <p class="form-p" type="How Did You Hear About Us?*"></p>
        <input type="checkbox" name="hearabout[]" value="Word Of Mouth">Word of Mouth<br>
        <input type="checkbox" name="hearabout[]" value="Friend / Relative">Friend / Relative<br>
        <input type="checkbox" name="hearabout[]" value="Google Search">Google Search<br>
        <input type="checkbox" name="hearabout[]" value="Social Media">Social Media<br>
        <input type="checkbox" name="hearabout[]" value="Newspaper">Newspaper<br>
       <input type="submit" value="send" name="submit_contact" class="submit-btn">
    </form>

我在php代码中是以这种格式编写的

<?php
if(isset($_POST['submit_contact']))  
{
require 'PHPMailer/PHPMailerAutoload.php';
$to = "xxxxk@gmail.com";
$name = $_POST['yourname'];
$mobilenumber = $_POST['mobilenumber'];
$email = $_POST['email'];
$address = $_POST['address'];
$hearabout = stripslashes($_POST['hearabout']);  

$message = array();    
$message[]='Name  :  '.trim($name).' ';
$message[]='Phone  :  '.trim($mobilenumber).' ';   
$message[]='Email  :  '.trim($email).' ';    
$message[]='Address  :  '.trim($address).' ';   
$message[]='How Did You hear About This?  :  '.trim($hearabout).' '; 
$message = implode('<br/>', $message);  

$mail->Subject    = "For Booking Milk";
$mail->Body = $message;
$mail->AddAddress($to); 
}
?> 

1 个答案:

答案 0 :(得分:1)

您正在将数组发送到$_POST中,并且stripslashes需要一个字符串才能工作。您可以implode将其更改为字符串。例如:

// glue together each element with a comma, then strip it
$hearabout = stripslashes( implode( ', ', $_POST['hearabout'] ) );

我仍然不确定您为什么使用stripslashes,但是您使用了。