Jump to content

[SOLVED] Loopsing threw checkbox array


dewey_witt

Recommended Posts

Here is some code..... I need to add the values only to a form input(text) on the page submitted too. I've done this a million times yet i keep getting the word "array" in front of the values as such......

 

 <input name="mail[]" type="checkbox" value="Value 1" /> 
<input name="mail[]" type="checkbox" value="Value 2" />
<input name="mail[]" type="checkbox" value="Value 3" />

 

Now this is what Im useing to retreive this data:

$mail = $_POST['mail'];

if(!empty($mail)) {
	foreach($mail as $key => $val) {
		$mail .= "". $val .", ";
}
}

        $stringa="$mail, ";
     
        $stringa=trim( ereg_replace( "[^@_.[:space:]a-zA-Z0-9]", " ", $stringa ) );
	echo $stringa;
	$send = "$stringa, ";

 

This is the result Im getting

For one I'd love to know why...Imma question asker and two anyone see wtf Im doing wrong? O.o

Link to comment
https://forums.phpfreaks.com/topic/78725-solved-loopsing-threw-checkbox-array/
Share on other sites

Looks to me liek your trying to get this

1,2,3 from your fields which.

$var = implode(",", $_POST[mail]); is the best option.

however try...

 

$mail =(is_array($_POST[mail]))? implode(",", $_POST[mail]):$_POST[mail];

 

if all fails, try this:

$count = count($_POST[mail]);
for($i=0;$i<$count;$i++) $mail.= ($i == $count)? $_POST[mail][$i]:$_POST[mail][$i].",";

You are assigning $mail to be equal to an array, then you are appending a string onto the end of that.  When php converts an array into a string(which is happening when you are appending a string to the end of $mail, which to start with is an array), it is converted to simply "Array". 

 

You can get rid of the "Array" at the beginning by simply not using $mail as the variable to store the $_POST['mail'] array.  In fact, why reassign that array to another variable anyway?

 

Use the recommendation by xyn:

 

$mail =(is_array($_POST['mail']))? implode(",", $_POST['mail']):$_POST['mail'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.