Multiple CheckBox Send to Email

Hi Guys,

I need help for Multiple Check-box Selected in a form.

My PHP Coding :

<?php $your_email ='[email protected]';// <

HTML Coding :

<?php if(!empty($errors)){ echo "

".nl2br($errors)."

"; } ?>

FIrst Name:

Last Name:

Email:

Contact Number:

Comments:
<?php echo htmlentities($user_message) ?>

What are you interested in doing?:

  • Web Desgin
  • <?php echo htmlentities($user_list) ?>
  • Web Hosting
  • <?php echo htmlentities($user_list) ?>
  • Web Applications
  • <?php echo htmlentities($user_list) ?>
  • Logo Design

    I am not understanding where I m making mistake , can anyone please help me.

    Welcome to our site! First, please do not double-post. I removed your duplicate post as it is not needed.
    Also, please use the PHP button above and place all your code between the PHP TAGS. This helps us grab
    the code to use in our editors and also is much much easier to read. Lastly, you should NOT place your email
    or passwords on ANY blog in the world. Robot scanners will read your post and take out your email to use
    in SPAM systems. I will remove it for you. Now onto your question…

    To debug your code, you can print the array for your list[] variable and see what is inside it. But, it looks
    very simple to use. You would just need to parse thru the list of options to see what the state of each is.
    Since you did not tell us what was not working or what you really want to do with the list of checkboxes,
    here is a simple sample of how to pull out your info from the list of ceckboxes…
    [php]
    // All checkboxes used a name of “list[]” and that is the array of your check boxes
    foreach ($_POST[‘list’] as $value) {
    echo $value;
    }
    [/php]
    This example just echos the values of the posted checkboxes. You would need to either create a list for
    the email or you can add text based on the values found. You need to remember that if you use a name
    such as list[] for the names of the checkboxes, that becomes an array of that group and you just have to
    parse thru the list using the foreach function.

    Hope that helps!

    Thanks for your time !

    THats works good but IF I select 2 or 3 values in result it showing only one value.

    For Example : Value = maths , Value = science , Value= social

    If select science and social …it is displaying only social.

    Thanks

    Newcomer, here is a sample page that shows how this works. It is a complete sample program. Just copy it
    as-is and place on your server with a name like test.php or whatever. Run it and it should work for you as I
    did test it. Hope you can use this sample to fix up your version. Let us know…
    [php]

    Checkbox Test... <?php echo "POST array contains: "; print_r($_POST); echo "

    "; // This shows you what this form is really posting to your code. It might help you learn about posted arrays...

    if(isset($_POST[‘submit’])) {
    if(empty($_POST[“list”])) {
    echo "Select at least one option. ";
    } else {
    // All checkboxes used a name of “list[]” and that is the array of your check boxes
    echo "You previously selected:
    ";
    foreach ($_POST[“list”] as $value) {
    echo $value . “
    ”;
    }
    }
    }
    ?>

    What are you interested in doing?:

    Web Design
    Web Hosting
    Web Apllication
    Logo Design


    [/php] Note that I had it display the contents of the entire posted values. This is in the form at a $_POST[] array. It might help you understand how PHP passes info from the browser. It should show you the submit button and all of the values you checked...

    Thanks for your time for helping me

    Still I am not getting the exact results

    If I select 1 value its gives 1 as output but when I select 3 values it will give only last value.

    I update my coding

    PHP

    <?php $your_email ='[email protected]';// <<=== update to your email address session_start(); $errors = ''; $name = ''; $email = ''; $subject = ''; $user_message = ''; $value = ''; if(isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $user_message = $_POST['message']; $value = $_POST['list']; ///------------Do Validations------------- if(empty($name) || empty($email)) { $errors .= "\n Name and Email are required fields. "; } if(empty($_POST['list'])) { echo "Select at least one option. "; } else { // All checkboxes used a name of "list[]" and that is the array of your check boxes echo "You previously selected:
    "; foreach ($_POST['list'] as $value) { echo $value . "
    "; } } if(IsInjected($email)) { $errors .= "\n Bad email value!"; } if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "\n The captcha code does not match!"; } if(empty($errors)) { //send the email $to = $your_email; $subject="Customer Information"; $from = $your_email; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $body = "A user $name submitted the Contact Form:\n". "Name: $name\n". "Email: $email \n". "Subject : $subject\n". "Message: \n ". "$user_message\n". "List: $value\n". "IP: $ip\n"; $headers = "From: $from \r\n"; $headers .= "Reply-To: $email \r\n"; mail($to, $subject, $body,$headers); header('Location: thanks-contact.html'); } } // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?>

    HTML

    <?php if(!empty($errors)){ echo "

    ".nl2br($errors)."

    "; } ?>

    Name:

    Email:

    Subject:

    Comments:
    <?php echo htmlentities($user_message) ?>

    What are you looking for?:

    Web Design
    Web Hosting
    Logo Design
    Web Application
    Web Matainace

    Javascript

    Well, Newcomer, did you actually look at the code I gave you and study how it works? The solution is there.

    First, you MUST post your code inside the PHP tags. It is annoying that you just place it in your post as a
    text list. It is harder for us to use the code that way. Please make sure for future posts that you place the
    code inside of the PHP tags. You just have to press the PHP button above the text area when you are doing
    your post and place your code inside of those PHP tags. Thank you for doing this from here on… Oh, also,
    if you have two or more files you are posting, place each inside separate PHP tags so we can copy them as
    separate files.

    Now, you have posted code. It pulls the list[] array. In my sample code, it just displays each that was in
    the array. In your code you want to place that info into a message that is emailed. Here is your code now
    in a PHP tags. In this way, the code has numbers that we can reference for our discussions…

    Your code all in one file:
    [php]

    <?php $your_email ='[email protected]';// <<=== update to your email address session_start(); $errors = ''; $name = ''; $email = ''; $subject = ''; $user_message = ''; $value = ''; if(isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $user_message = $_POST['message']; $value = $_POST['list']; ///------------Do Validations------------- if(empty($name) || empty($email)) { $errors .= "\n Name and Email are required fields. "; } if(empty($_POST['list'])) { echo "Select at least one option. "; } else { // All checkboxes used a name of "list[]" and that is the array of your check boxes echo "You previously selected:
    "; foreach ($_POST['list'] as $value) { echo $value . "
    "; } } if(IsInjected($email)) { $errors .= "\n Bad email value!"; } if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "\n The captcha code does not match!"; } if(empty($errors)) { //send the email $to = $your_email; $subject="Customer Information"; $from = $your_email; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $body = "A user $name submitted the Contact Form:\n". "Name: $name\n". "Email: $email \n". "Subject : $subject\n". "Message: \n ". "$user_message\n". "List: $value\n". "IP: $ip\n"; $headers = "From: $from \r\n"; $headers .= "Reply-To: $email \r\n"; mail($to, $subject, $body,$headers); header('Location: thanks-contact.html'); } } // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?>

    <?php if(!empty($errors)){ echo "

    ".nl2br($errors)."

    "; } ?>

    Name:

    Email:

    Subject:

    Comments:
    <?php echo htmlentities($user_message) ?>

    What are you looking for?:

    Web Design
    Web Hosting
    Logo Design
    Web Application
    Web Matainace

    Javascript

    [/php]
    So, as you see in line # 66 you have this: “List: $value\n”.
    This is where you pull ONE value from your variable $value and put it into your message. So, your code is
    doing what you told it to do. It does not LOOP thru the list[] array and display all of the items inside it, it
    just displays the last item that you stored into the $value variable…

    There are two ways to fix this. You can place a loop inside the area where you create the message to pull
    out all of the values or, you can do that ahead of time in a separate code area and create a string that holds
    all of your list[] array contents… Either way should work. Here is one way to do it. Replace the section
    where you are creating the $body of your message with this:
    [php]
    $body = “A user $name submitted the Contact Form:\n”.
    “Name: $name\n”.
    “Email: $email \n”.
    “Subject : $subject\n”.
    "Message: \n ".
    //---------------------------------------------------
    "List: ";
    foreach ($_POST[“list”] as $value) {
    $body .= $value . “\n”;
    }
    //---------------------------------------------------
    $body .= “$user_message\n”.
    “List: $value\n”.
    “IP: $ip\n”;
    [/php]
    I did not test this, but, it should work. Basically, I replaced where you placed one value from the list[] array
    into your $body of the message and placed all of them there. I put a blank line above them and below so
    they stand out, not needed in your live code… Note that you can use a string such as $body and add to it
    as needed. the " .= " means to concatenate or add-to-end-of-string. So, my small change in your code
    adds all of the list[] entries and then continues with the rest of your creation of the $body message.

    Hope that makes sense to you… Good luck!

    That was Super my friend.

    Thanks for time and your help.

    I did a little bit modified

    Thanks

    Glad I could help, Newcomer! It is always a nice warm feeling to solve a programming puzzle!

    We will see you again when you hit the next puzzle!

    Sponsor our Newsletter | Privacy Policy | Terms of Service