domdeez Posted February 2, 2008 Share Posted February 2, 2008 I've set up a simple multi-page form. I am passing the form data from page to page using hidden fields. I do so by adding the piece of code listed below between the form tags of the receiving page. <?php foreach($_POST as $key=>$value){ if ($key!="submit"){ $value=htmlentities(stripslashes(strip_tags($value))); echo "\t<input type=\"hidden\" name=\"$key\" value=\"$value\">\n"; }if (is_array($value)) { $value = implode(",",$value); } } ?> Here's the problem. On the first page there are checkboxes with the same name but different values. The hidden fields that appear on page 2 only show the first value of this checkbox (when multiple boxes are selected). I've seen solutions like this below, but haven't found a way to get them to work with the code above. Can someone point me in the right direction? Thanks. Retrieving Multiple Checkbox Values As An Array If brackets are missing on names; ... <INPUT TYPE="checkbox" NAME="s" VALUE="A" CHECKED>Checkbox 1<BR> <INPUT TYPE="checkbox" NAME="s" VALUE="B">Checkbox 2<BR> <INPUT TYPE="checkbox" NAME="s" VALUE="C">Checkbox 3<BR> ... processing script <?php echo '<PRE>('.(gettype($_POST['s']).') '; print_r($_POST['s']); ?> will produce: (string) A But if you add [] after each element’s name, ... <INPUT TYPE="checkbox" NAME="s[]" VALUE="A" CHECKED>Checkbox 1<BR> <INPUT TYPE="checkbox" NAME="s[]" VALUE="B">Checkbox 2<BR> <INPUT TYPE="checkbox" NAME="s[]" VALUE="C">Checkbox 3<BR> ... the same PHP code will produce the output below: (array) Array ( [0] => A [1] => B [2] => C ) Quote Link to comment https://forums.phpfreaks.com/topic/88997-checkbox-same-name-different-value-problem/ Share on other sites More sharing options...
Cep Posted February 2, 2008 Share Posted February 2, 2008 Why wouldn't you just use sessions? By the way this line, $value=htmlentities(stripslashes(strip_tags($value))); Is ridiculous use this, $value=htmlspecialchars($value, ENT_QUOTES); Quote Link to comment https://forums.phpfreaks.com/topic/88997-checkbox-same-name-different-value-problem/#findComment-455794 Share on other sites More sharing options...
domdeez Posted February 2, 2008 Author Share Posted February 2, 2008 Thanks for the response. I'll see if I can work it out using sessions. New to PHP. Quote Link to comment https://forums.phpfreaks.com/topic/88997-checkbox-same-name-different-value-problem/#findComment-455827 Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 keep the [] in the form makes it easier to process [] will return an array, and the function u posted will handle numerics or strings so build a function function createhidden($post,$inp='') { foreach($post as $key => $val) { if(is_array($val)) createhidden($val,$key); else { $val=htmlentities(stripslashes(strip_tags($val))); echo '<INPUT TYPE=HIDDEN name="' . (!empty($inp)?"$inp\[\]":$key) . "\" VALUE=\"$val\">\n"; } } notice it calls itself on arrays so ya can call it up as createhidden($_POST); Quote Link to comment https://forums.phpfreaks.com/topic/88997-checkbox-same-name-different-value-problem/#findComment-455843 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.