PHP form question

Posted on 2007-10-11
Last Modified: 2013-12-13
Hi - I have two different arrays of checkboxes in a form:

<input name='service[]' value='abcde' type='checkbox' />


<input name='extra[]' value='abcde' type='checkbox' />

There are also other form elements that I check and put into strings to eventually email like so:

/Set recipient email
$myemail = "";

//See if the form has been submitted

 //Assign the text fields to variables
 $name = $_POST['name'];
 $position = $_POST['position'];
 $company = $_POST['company'];
 $address = $_POST['address'];
 $postcode = $_POST['postcode'];
 $phone = $_POST['phone'];
 $fax = $_POST['fax'];
 $email = $_POST['email'];
 $quote = $_POST['quote'];
 $further = $_POST['further'];
 $other = $_POST['other'];

 //Now to validate the form
 $errors = array();
 if($name == ""){
        array_push($errors, "You did not enter your name!");
 if($email == ""){
        array_push($errors, "You did not enter an email address!");
 else if(ereg("[A-Za-z0-9_-]+([.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([.]{1}[A-Za-z0-9-]+)+", $email)) {
        $emailadd = "valid";
 else {
        $emailadd = "invalid";
 if ($emailadd == "invalid") {
        array_push($errors, "That does not appear to be a valid email address!");

 //If there are no errors then send the email
 if(count($errors) == 0){
        $to = "$myemail";
        $subject = "Enquiry";
        $body = "Name: $name \n Email: $email \nPosition: $position \nCompany: $company \nAddress: $address \nPostcode: $postcode \nPhone no: $phone \nFax no: $fax  \n\n$quote ";
        $header = "From: $email";
        mail($to, $subject, $body, $header);

Now, my question is, how do I add each selected checkbox and its' value into a string to email correctly? Taking into account that there are two groups of checkboxes?

Many thanks.

Question by:Eternal_Student
    LVL 15

    Accepted Solution

    Is this sufficient
    $frm_arr_services = $_POST['service'];

    $tmp_str_services = "Selected services are ". implode(",",$frm_arr_services);

            $body = "Name: $name \n Email: $email \nPosition: $position \nCompany: $company \nAddress: $address \nPostcode: $postcode \nPhone no: $phone \nFax no: $fax  \n\n$quote " . $tmp_str_services;

    In the same way you can do for the extras
    LVL 1

    Expert Comment

    Why do you give it the name "service[]" ? Just name it "service" to make it easier on yourself.

    Now, if it's selected:

    echo "My output....";
    if (isset($_GET['abcde'])) { echo "Checkbox was selected..."; }

    Does this help?

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Write an app 9 37
    code igniter select query 7 21
    Setting up a WAMP server... 7 19
    Add code to body in wordpress site 8 10
    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now