Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


PHP form question

Posted on 2007-10-11
Medium Priority
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 = "email@hotmail.com";

//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

babuno5 earned 2000 total points
ID: 20055640
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

Expert Comment

ID: 20063598
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

580 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