Hello, I have a form that has several checkboxes. It looks similar to this:

<input name="service_needed[]" type="checkbox" value="service1" class="checkbox" />
<input name="service_needed[]" type="checkbox" value="service2" class="checkbox" />
<input name="service_needed[]" type="checkbox" value="service3" class="checkbox" />

I'm trying to store the values of whichever boxes are checked inside of one variable so that I can use that variable in the mail(); function.

I know I have to use a foreach to print them out similar to this:

foreach ($_POST['service_needed'] as $Services) {
       echo $Services;
}

....But then how I do store what the loop prints out inside of a variable? If I use $Services, it only gives me the last checkbox checked value. I need all boxes values that are checked stored inside of one variable.

Thanks for any help.

Hi Andrew, if you wanted them all into one string variable, you could do this:

// Bind the posted array of checkbox elements to a variable.
$service_needed = $_POST['service_needed'];

// We use implode to separate the array with a comma and a space.
$allCheckboxes = implode(", ", $service_needed);

// Echo out the string for you to see.
echo $allCheckboxes;

Try that!

Anthony

Hi Andrew, if you wanted them all into one string variable, you could do this:

// Bind the posted array of checkbox elements to a variable.
$service_needed = $_POST['service_needed'];

// We use implode to separate the array with a comma and a space.
$allCheckboxes = implode(", ", $service_needed);

// Echo out the string for you to see.
echo $allCheckboxes;

Try that!

Anthony

Thank you Anthony! That worked great.

It's amazing how soon I forget little things like that. I just started PHP in January and we learned Implode/Explode in the 2nd week of class I believe, but I completely forgot about it.

Thank you again.

Andrew

Hi Andrew, if you wanted them all into one string variable, you could do this:

// Bind the posted array of checkbox elements to a variable.
$service_needed = $_POST['service_needed'];

// We use implode to separate the array with a comma and a space.
$allCheckboxes = implode(", ", $service_needed);

// Echo out the string for you to see.
echo $allCheckboxes;

Try that!

Anthony

Try this ....

index.php
---------------------------------------------------

<form action="process.php" method="post">
<input name="service_needed[]" type="checkbox" value="service1" class="checkbox" />
<input name="service_needed[]" type="checkbox" value="service2" class="checkbox" />
<input name="service_needed[]" type="checkbox" value="service3" class="checkbox" />
<input type="submit" name="Submit" value="Submit" />
</form>

process.php
---------------------------------------------------

<?php

$count  = count($_POST["service_needed"]);

for($i=0;$i<$count;$i++)
{
	$newvarible[$i] = $_POST["service_needed"][$i]; 
} 

echo "The First Checked Value is<b>    ".$newvarible[0]."   </b><br>";
echo "The Sec Checked Value is<b>      ".$newvarible[1]."   </b><br>";
echo "The Third Checked Value is<b>    ".$newvarible[2]."   </b><br>"; 

?>

Not a problem pal, everyone's here to help! Mark this thread as solved, and happy PHP coding!

Until next time,

Anthony

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.