0

I have written a code to send multiple selected checkboxes value in an array but getting as blank in email.

Here is the code i have written in 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>

And in Php code i have written in this format

<?php
if(isset($_POST['submit_contact']))  
{
require 'PHPMailer/PHPMailerAutoload.php';
$to = "[email protected]";
$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); 
}
?> 
8
  • write what you get with var_dump($_POST) Commented Jul 6, 2019 at 9:07
  • $_POST['hearabout'] would be an array, stripslashes takes a string. try explode(",", $POST['hearabout']); instead! Commented Jul 6, 2019 at 9:13
  • What is the type attribute and why are you not putting the text in-between the tags? Are you wrapping all the inputs inside of a <form>? Commented Jul 6, 2019 at 9:28
  • @Jeff getting blank changed that Commented Jul 6, 2019 at 9:29
  • @krimaeus yes i am wrapping all the input fields inside a form itself Commented Jul 6, 2019 at 9:29

1 Answer 1

1

You are sending an array into $_POST, and stripslashes requires a string to work. You can change it into a string by implodeing it. For example:

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

I'm still not sure why you are using stripslashes, but you do you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.