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

Arraydewey_witt@yahoo.com, David_someone@somewhere.com,

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

Link to comment
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].",";

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.