Jump to content

Checkbox $_POST remember


mikenl

Recommended Posts

Hi,

 

I generate a list of checkboxes like this:

 

// here I set an array, $rows is db query result

$language = array(1 => "Afrikaans","Albanian","Arabic","Armenian","Bangla","Bosnian","Bulgarian","Burmese","Catalan","Cebuano","Chinese","Croatian","Czech","Danish","Dutch","English","Esperanto","Estonian","Finnish","French","Gaelic","German","Greek","Hebrew","Hindi","Hungarian","Icelandic","Indonesian","Italian","Japanese","Korean","Kurdish","Latin","Latvian","Lithuanian","Luxembourgish","Macedonian","Malay","Norwegian","Persian","Polish","Portuguese","Romanian","Romansh","Russian","Serbian","Sign Language","Slovak","Slovenian","Sorbian","Spanish","Swedish","Swiss German","Tagalog","Thai","Turkish","Ukrainian","Welsh");

loadform2($language,$rows,'lang');

// here I create the checkbox list

function loadform2($values,$rows,$fieldname){

if($_POST){
	$output = $_POST;
	} else {
	$output = $rows;
}

$size = count($values);
?>
<div style="width:125px;height:100px;overflow:auto;margin-bottom:7px; padding:0">
  <?

foreach ($values as $key => $value){
	if(!empty($key)){
		echo "<input id=\"$fieldname".$key."\" name=\"$fieldname"."[]"."\" type=\"checkbox\" value=\"$key\"";
	if($_POST){

// how do I make this work? $output is $_POST data
				if ($output[0][$fieldname][$key] == $key-1) echo "checked";
				} else {
// this works, $output comes from db query
				if ($output[0][$fieldname.$key] == 1) echo "checked";
				}
		echo "/><label for=\"$fieldname".$key."\">$value</label><br />\n";
 		}
 	}		
 ?>
  <input name="<? echo $fieldname ?>_num" type="hidden" value="<? echo $size ?>" />

</div>
<?
}

 

I want to have all checkboxes that were checked when POSTed to 'stick' but don't know how to do this for $_POST data...

 

Anyone knows how to handle this?

Link to comment
Share on other sites

print_r of db query result:

 

[lang1] => 1 [lang2] => 1 [lang3] => 0 [lang4] => 0 [lang5] => 0 [lang6] => 0 [lang7] => 0 [lang8] => 0 [lang9] => 0 [lang10] => 0 [lang11] => 0 [lang12] => 0 [lang13] => 0 [lang14] => 0 [lang15] => 0 [lang16] => 0 [lang17] => 0 [lang18] => 0 [lang19] => 0 [lang20] => 0 [lang21] => 0 [lang22] => 0 [lang23] => 0 [lang24] => 0 [lang25] => 0 [lang26] => 0 [lang27] => 0 [lang28] => 0 [lang29] => 0 [lang30] => 0 [lang31] => 0 [lang32] => 0 [lang33] => 0 [lang34] => 0 [lang35] => 0 [lang36] => 0 [lang37] => 0 [lang38] => 0 [lang39] => 0 [lang40] => 0 [lang41] => 0 [lang42] => 0 [lang43] => 0 [lang44] => 0 [lang45] => 0 [lang46] => 0 [lang47] => 0 [lang48] => 0 [lang49] => 0 [lang50] => 0 [lang51] => 0 [lang52] => 0 [lang53] => 0 [lang54] => 0 [lang55] => 0 [lang56] => 0 [lang57] => 0 [lang58] => 1 

Link to comment
Share on other sites

  • 1 month later...

Here's a slight rework that should work

 

<?php

$options = array(1 => "Afrikaans","Albanian","Arabic","Armenian","Bangla","Bosnian","Bulgarian","Burmese","Catalan","Cebuano","Chinese","Croatian","Czech","Danish","Dutch","English","Esperanto","Estonian","Finnish","French","Gaelic","German","Greek","Hebrew","Hindi","Hungarian","Icelandic","Indonesian","Italian","Japanese","Korean","Kurdish","Latin","Latvian","Lithuanian","Luxembourgish","Macedonian","Malay","Norwegian","Persian","Polish","Portuguese","Romanian","Romansh","Russian","Serbian","Sign Language","Slovak","Slovenian","Sorbian","Spanish","Swedish","Swiss German","Tagalog","Thai","Turkish","Ukrainian","Welsh");
$selections = ($_POST['languages'] ? $_POST['languages'] : array());

function checkList($options, $selections, $name) {
echo "<div style='width:500px;height:200px;overflow:auto;margin-bottom:7px; padding:0'>";
foreach ($options as $key => $value){
	echo "<input id='". $name . $key ."' name='". $name ."[]' type='checkbox' value='". $key ."' ". (in_array($key, $selections) ? "checked" : "") .">";
	echo "<label for='". $name . $key ."'>$value</label><br />\n";
}
echo "</div>";
}

?>

<form method='post'>
<?php checkList($options, $selections, 'languages'); ?>
<input type='submit'>
</form>

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.