0

I could not figure it out what goes wrong. I need to pass the value of my checkbox( if is checked or not checked) , from page1.php to page2.php. Is only just one checkbox, not an array. Mention that either form action I cannot use.

This is the code from page1.php:

<?php
   session_start();
   $checked = 0;  // not checked
   $_SESSION['first'] = $checked;
   echo "<input value='$checked'  name='name' type='checkbox'> <span class='description'> Enable this?</span>";

 ?>

this is the code from page2.php:

<?php   

   session_start();
   $checked = $_SESSION['first'];
   if ( $checked == 1  ) {

       echo ' checkbox checked '; 
   }
   else 

       echo ' checkbox not checked ';           

?>
cadobe
  • 37
  • 1
  • 2
  • 8

2 Answers2

0

The original value of $checked:

$checked = 0;

While:

input value='$checked'

Result when checked:

$_POST['name'] == '0'

Then you're incorrectly checking if it equals 1... The value of the checkbox is what gets sent if it is checked. Thus you should be checking for '0' to catch when it is selected.

Example:

HTML: <input type="checkbox" name="foo" value="bar" />

PHP: if ($_POST['foo'] == 'bar') { // do stuff }

To then display the checkbox status on subsequent pages instead of setting it's value, you set it's checked status.

HTML5 Specs (same for older):

checked = "checked" or "" (empty string) or empty

Specifies that the element represents a selected control.

e.g. <input type="checkbox" name="foo" value="bar" checked />

http://www.w3.org/TR/html-markup/input.checkbox.html

MrYellow
  • 426
  • 6
  • 23
0

1 . you should set your form method to post 2 . set checkbox value in session , as i use in below 3 . if your checkbox click , it will get value 1 which is defined in $checkbox variable , if not check it will get zero 4. if it check and submit the form , then you can set checkbox value in session variable your page1.php will have this code

<?php  session_start();

if(isset($_POST)) { 
   $_SESSION['first'] = $_POST['name']; // checkbox name
}

   $checked = 1;
   echo "<input value='$checked'  name='name' type='checkbox'> <span class='description'> Enable this?</span>";
?>

and your page2.php

<?php   session_start();


   $checked = $_SESSION['first'];
   if ( $checked == 1  ) {

       echo ' checkbox checked '; 
   }
   else {
       echo ' checkbox not checked ';           
  }

?>
Noman
  • 4,088
  • 1
  • 21
  • 36
  • Noman, that's great. It works excellent. I forgot to store the value on POST for the first page. Thanks a lot. – cadobe Aug 12 '14 at 19:56
  • I would like to be back at my question for some improvement. The solution provided by Noman is perfect, but if I refresh the browser or close and open it again, the information is gone for ever. Is that possible to save the value into a database and how? Any help would be nice. – cadobe Aug 12 '14 at 23:56
  • yes you can save the values into database , simply write your query under the session save the values from checkbox. right after $_POST['name']; start new line and put it there , do not forget to add your database connection file , or you can put the connection in above (page1.php).. dont forget to vote me up :D – Noman Aug 14 '14 at 18:19