1

i have a form in three steps, from step 1 to step two there is a checkbox, i would like to keep the checkbox value in a session and also to show if it's checked or unchecked even if the user goes to step 3 of the form then comes back to step two, right now on form submit i have all the $_POST vars in $_SESSION vars but i cant make it work with checkboxes, this is what i got right now:

   <input type="checkbox" value="<?php  echo isset($_POST['afficher_ddn'])? "1":"0";  ?>"
   style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn" 
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />

but this doesnt work.

user7832
  • 51
  • 8

3 Answers3

1
<input type="checkbox" value="1" style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn" 
<?php echo isset($_SESSION['afficher_ddn']) && $_SESSION['afficher_ddn'] == "1" ? 'checked="checked"' : ''; ?> />

For one, you don't change the value of the checkbox. If it is not checked it won't pass to the post vars. See here: Does <input type="checkbox" /> only post data if it's checked?

Now, the current standard for checked is checked="checked" I have no idea where or why this standard became one at the firm I work for, but it's what we do so I relay that here.

If the code provided here does not work for you, I would var_dump($_SESSION) to make sure it's set as well as check the actual HTML in something like firebug (firefox) or developer tools in chrome. Sometimes goofiness happens and checked="checked" is actually set in the html but doesn't display in browser. In those times I usually yank out some hair and clear caches. then it usually clears up.

Community
  • 1
  • 1
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
1

Your code is fine.Possibly there is some problem in passing the the value in session as i am checking the code manually it is working perfectly: use print_r($_SESSION) for testing and post the output;If possible provide all the forms:

Try Below Code:

   <input type="checkbox" value="1"
   style="margin-left: 20px;" name="afficher_ddn" id="ddn" 
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> /> 
Vikas Umrao
  • 2,800
  • 1
  • 15
  • 23
  • this is the var dump so the checkbox is there but still not working `Array ( [nom] => sdfs [prenom] => sdf [adresse] => sdf [code_postal] => sdf [ville] => sdf [JJ] => 20 [MM] => 06 [AAAA] => 1970 [tel] => sdfdsf [linkedin] => [viadeo] => [site_web] => [submit_step_1] => suivant → [civilite] => Mme [name] => [tmp_name] => [step] => Array ( [2] => 2 ) [afficher_ddn] => 1 ) ` – user7832 Nov 07 '12 at 19:36
  • is that $_SESSION or $_POST? I don't see ddn in there. There's afficher_ddn but that's not the same thing. – Kai Qing Nov 07 '12 at 19:38
  • oop sorry the field is afficher_ddn – user7832 Nov 07 '12 at 19:42
  • just to be clear, the html input is also named afficher_ddn and you just shortened it to ddn for this example, right? – Kai Qing Nov 07 '12 at 19:44
  • right, now everything is back to normal but it still not working. – user7832 Nov 07 '12 at 19:45
  • again post the var dump variable – Vikas Umrao Nov 07 '12 at 19:46
  • here `Array ( [nom] => sdfs [prenom] => sdf [adresse] => sdf [code_postal] => sdf [ville] => sdf [JJ] => 20 [MM] => 06 [AAAA] => 1970 [tel] => sdfdsf [linkedin] => [viadeo] => [site_web] => [submit_step_1] => suivant → [civilite] => Mme [name] => [tmp_name] => [step] => Array ( [2] => 2 ) [afficher_ddn] => 1 ) ` – user7832 Nov 07 '12 at 19:52
  • ok i deleted the PHPsessID and tried your code, strange thing is check or unchecked the var dump of the checkbox is always this: `[afficher_ddn] => 0` – user7832 Nov 07 '12 at 20:02
  • now it's always [afficher_ddn] => 1 and the checkbox is always checked, i really dont get it, thanks for your help anyway. – user7832 Nov 07 '12 at 20:13
0

try this example separetly

Make a page test.php as:

    <?php
    session_start();
    ?>
    <form methos='post' method='post' action='test1.php'>
       <input type="checkbox" value="1"
       style="margin-left: 20px;" name="afficher_ddn" id="ddn"
    <?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> /><br>
Name:<input type='text' name='f1' value='<?php echo $_SESSION['f1'] ?>' ><br>
Select:<select name='s1'>
<option value="" <?php echo $_SESSION['s1']=="" ? "selected" : ""; ?>></option>
<option value="1" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>1</option>
<option value="2" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>2</option>
</select><br>
Test:
<textarea name='tex1'><?php echo $_SESSION['tex1']; ?></textarea>

    <input type='submit'  value='submit'>
    </form>

Make a second page test1.php as:

    <?php
    session_start();
    print_r($_POST);
    echo $_SESSION['afficher_ddn']=$_POST['afficher_ddn'];
echo $_SESSION['f1']=$_POST['f1'];
echo $_SESSION['s1']=$_POST['s1'];
echo $_SESSION['tex1']=$_POST['tex1'];
    ?>
    <form methos='post' action='third.php'>

    <a href='test.php'>Step1</a>
    </form>

and Check

Vikas Umrao
  • 2,800
  • 1
  • 15
  • 23
  • ok so here the logic works so it must be something in my file , the strange thing is that it only involves checkboxes. – user7832 Nov 07 '12 at 20:32
  • hi again!, with your help i made it work! the code is ugly and could be optimized i think, so putting this: `" style="margin-left: 20px;" id="afficher_ddn" /> ` the logic works perfectly, if i check i get a `[afficher_ddn] => 1`, if i dont check i get a `[afficher_ddn] =>` and previous state of checkbox is remembered. – user7832 Nov 07 '12 at 21:44
  • this is better ` />` – user7832 Nov 07 '12 at 22:00