Newbie question how do i echo a checkbox value to next page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

birken
Forum Newbie
Posts: 9
Joined: Mon Jan 23, 2012 1:44 pm

Newbie question how do i echo a checkbox value to next page?

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie question how do i echo a checkbox value to next p

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
birken
Forum Newbie
Posts: 9
Joined: Mon Jan 23, 2012 1:44 pm

Re: Newbie question how do i echo a checkbox value to next p

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie question how do i echo a checkbox value to next p

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
birken
Forum Newbie
Posts: 9
Joined: Mon Jan 23, 2012 1:44 pm

Re: Newbie question how do i echo a checkbox value to next p

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie question how do i echo a checkbox value to next p

Post by social_experiment »

The sample only returns the array, to display the data within the array you could use a foreach() statement;
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
birken
Forum Newbie
Posts: 9
Joined: Mon Jan 23, 2012 1:44 pm

Re: Newbie question how do i echo a checkbox value to next p

Post by birken »

sorry for being stupid :oops: where do i add that foreach()

warm reagrds birken
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie question how do i echo a checkbox value to next p

Post 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 />';
  }  
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
birken
Forum Newbie
Posts: 9
Joined: Mon Jan 23, 2012 1:44 pm

Re: Newbie question how do i echo a checkbox value to next p

Post 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 :oops:

Reagrds Birken
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie question how do i echo a checkbox value to next p

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
birken
Forum Newbie
Posts: 9
Joined: Mon Jan 23, 2012 1:44 pm

Re: Newbie question how do i echo a checkbox value to next p

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie question how do i echo a checkbox value to next p

Post 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 />';
  }  
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
birken
Forum Newbie
Posts: 9
Joined: Mon Jan 23, 2012 1:44 pm

Re: Newbie question how do i echo a checkbox value to next p

Post by birken »

hi again,

same error: Warning: Invalid argument supplied for foreach()
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Newbie question how do i echo a checkbox value to next p

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
birken
Forum Newbie
Posts: 9
Joined: Mon Jan 23, 2012 1:44 pm

Re: Newbie question how do i echo a checkbox value to next p

Post 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
Post Reply