Page 1 of 2
Newbie question how do i echo a checkbox value to next page?
Posted: Mon Jan 23, 2012 1:52 pm
by birken
Hi guys,
i am new to this forum and i am super newbie i wonder if any one could give me a hand on this.
i have this site where users check boxes on my site, and i want to pass the value to another page but not all the values.
i only want to pass the air conditioning, private pool and shared pool if they are checked.
here is the code:
Code: Select all
<?
$arr=array("hob/stove","oven","fridge/freezer","fridge","microwave","dishwasher","washing machine","tumble drye","iron/ironing board","cable tv","satellite tv","tv","telephone","internet access","central heating","air conditioning","room fans","cot","high chair","barbecue","private pool","pool heating","shared pool","pets accepted");
foreach($arr as $val){
$val1=$this->nl->facility_id;
if($val){
$vv=explode(",",$val1);
}
$check='';
if(in_array($val,$vv)){
$check="checked=\"\"";
}
?>
<input type="checkbox" name="facility_id[]" value="<?=$val?>" <?=$check?>>
<?=$val?><br />
<?
}
?>
Warm regards Birken
Re: Newbie question how do i echo a checkbox value to next p
Posted: Mon Jan 23, 2012 3:24 pm
by social_experiment
$_POST['facility_id'] is an array so you can use
in_array() to see if the value you want have been selected
http://www.php.net/manual/en/function.in-array.php
Re: Newbie question how do i echo a checkbox value to next p
Posted: Mon Jan 23, 2012 3:31 pm
by birken
hi social_experiment,
thx for taking time tolok at my question as i am super newbie is it possible to show exactly how the whole code should look so i can paste it.
Warm regards
Re: Newbie question how do i echo a checkbox value to next p
Posted: Mon Jan 23, 2012 3:41 pm
by social_experiment
Code: Select all
<?php
if (is_array($_POST['facility_id'])) {
foreach ($_POST['facility_id'] as $value) {
if ($value == 'air conditioning' || $value == 'private pool' || $value == 'shared pool') {
$ary[] = $value;
}
}
return $ary;
}
?>
This may work easier than in_array(); The code loops through the $_POST['facility_id'] array and if any of the values match the specified values it gets added into an array called $ary; this is returned after the foreach loop and can be used as you wish; the code is untested so post any problems / questions should they arise
hth
Re: Newbie question how do i echo a checkbox value to next p
Posted: Mon Jan 23, 2012 4:02 pm
by birken
thx again
i added the code and i get no Error and also nothing show up on page.
any ideas?
i appreciate your help.
Re: Newbie question how do i echo a checkbox value to next p
Posted: Mon Jan 23, 2012 4:41 pm
by social_experiment
The sample only returns the array, to display the data within the array you could use a foreach() statement;
Re: Newbie question how do i echo a checkbox value to next p
Posted: Mon Jan 23, 2012 4:58 pm
by birken
sorry for being stupid

where do i add that foreach()
warm reagrds birken
Re: Newbie question how do i echo a checkbox value to next p
Posted: Tue Jan 24, 2012 12:19 am
by social_experiment
No problem
Code: Select all
<?php
if (is_array($_POST['facility_id'])) {
foreach ($_POST['facility_id'] as $value) {
if ($value == 'air conditioning' || $value == 'private pool' || $value == 'shared pool') {
$ary[] = $value;
}
}
return $ary;
}
// loop through the array
foreach ($ary as $result) {
echo $result . '<br />';
}
?>
Re: Newbie question how do i echo a checkbox value to next p
Posted: Tue Jan 24, 2012 2:33 am
by birken
Godmorning social_experiment
i adde the code but seem not to work i get: Warning: Invalid argument supplied for foreach()
and thx again for your time.
any ideas
Reagrds Birken
Re: Newbie question how do i echo a checkbox value to next p
Posted: Tue Jan 24, 2012 2:44 am
by social_experiment
Did you paste it on the page you posted or the page that handles the submission, it should go on the latter page because $_POST['facility_id'] need to be processed first
Re: Newbie question how do i echo a checkbox value to next p
Posted: Tue Jan 24, 2012 3:08 am
by birken
Hi, i posted on the page where the result shoul show up. i am confused

how will the result show if is not on result page?
Reagrds B
Re: Newbie question how do i echo a checkbox value to next p
Posted: Tue Jan 24, 2012 3:12 am
by social_experiment
Code: Select all
<?php
if (is_array($_POST['facility_id'])) {
foreach ($_POST['facility_id'] as $value) {
if ($value == 'air conditioning' || $value == 'private pool' || $value == 'shared pool') {
$ary[] = $value;
}
}
// comment or remove the part below this
//return $ary;
}
// loop through the array
foreach ($ary as $result) {
echo $result . '<br />';
}
?>
Re: Newbie question how do i echo a checkbox value to next p
Posted: Tue Jan 24, 2012 3:23 am
by birken
hi again,
same error: Warning: Invalid argument supplied for foreach()
Re: Newbie question how do i echo a checkbox value to next p
Posted: Tue Jan 24, 2012 3:58 am
by social_experiment
Had a look at the code i pasted and it's working; I'm guessing that $_POST['facility_id'] is not yet an array when the code is being run meaning you need to submit the form because on the form page itself $_POST['facility_id'] is not yet an array so if you have the code there (without checking if the submit button has been clicked) you are likely to experience errors. Don't use short tags (<? ?>) rather use <?php and to close use ?>. Paste the code you are currently using including the sample i gave
Re: Newbie question how do i echo a checkbox value to next p
Posted: Tue Jan 24, 2012 4:35 am
by birken
Now i am lost
ok let me explain what i have done.
i have page one where user add there details and one of the details is checkboxes here is the code code for page one:
Code: Select all
<?php
$arr=array("hob/stove","oven","fridge/freezer","fridge","microwave","dishwasher","washing machine","tumble drye","iron/ironing board","cable tv","satellite tv","tv","telephone","internet access","central heating","air conditioning","room fans","cot","high chair","barbecue","private pool","pool heating","shared pool","pets accepted");
foreach($arr as $val){
$val1=$this->nl->facility_id;
if($val){
$vv=explode(",",$val1);
}
$check='';
if(in_array($val,$vv)){
$check="checked=\"\"";
}
?>
<input type="checkbox" name="facility_id[]" value="<?=$val?>" <?=$check?>>
<?=$val?><br />
<?php
}
?>
on page two where the users details shows up i have added your code:
Code: Select all
<?php
if (is_array($_POST['facility_id'])) {
foreach ($_POST['facility_id'] as $value) {
if ($value == 'air conditioning' || $value == 'private pool' || $value == 'shared pool') {
$ary[] = $value;
}
}
// comment or remove the part below this
//return $ary;
}
// loop through the array
foreach ($ary as $result) {
echo $result . '<br />';
}
?>
i dont know if i have done the right thing but i get the same error Warning: Invalid argument supplied for foreach()
or do you mean that both of the codes should be in the same file?
reagrds Birken