4cu5 4cu5 - 23 days ago 6
PHP Question

multi step form, remember checkbox values in second form after submitting first one

I have multistep form by using jquery Tabs, each step or Tab have different form
with action (action='reg.php#tabs-1') and so on.. up to the end.
there is also Save button in each tab, which remember selected values in SESSION .

tab1: have select input with two values for example (option1, option2)

tab2: have checkboxes, which are showing depending on selected section (option1 or option2) in tab1 (if selected option1 in tab1, then show checkboxes in tab2 with name='a' and name='b' OR if selected option2 in tab1, then show checkboxes in tab2 with name='x' and name='z')

I remembering those values after pressing Save button in tab2 in SESSIONS as well.

The problem is: when I tick one of the checkboxes in tab2, saving them, and go back to tab1, changing any values other from select and clicking Save in tab1, my checked checkbox values in tab2 become unchecked

but I steel want them to be checked

php:

<?php session_start(); if (!empty($_SESSION['animal'])){if($_SESSION['animal']=='1'){$selected1='selected';$selected2='';} else {$selected2='selected';$selected1='';}}if (!empty($_POST['animal'])){if ($_POST['animal']=='1'){$selected1='selected';$selected2='';$_SESSION['animal']=$_POST['animal'];}else {$selected2='selected';$selected1='';$_SESSION['animal']=$_POST['animal'];}} if ($_POST['a']=='dog'){$_SESSION['a']=$_POST['a'];$checked1='checked';} else { $checked1='';unset ($_SESSION['a']);}if ($_POST['z']=='horse'){ $_SESSION['z']=$_POST['z']; $checked2='checked';} else {$checked2='';unset ($_SESSION['z']);}?>


script:

$(function() {$( "#tabs" ).tabs();});


html:

<script src="js/jquery.js"></script><link rel="stylesheet" href="js/jquery.custom/jquery-ui.css"><script type="text/javascript" src="js/jquery.custom/jquery-ui.min.js"></script><div id="tabs">
<ul>
<li><a id="step1" href="#tabs-1">step 1</a></li>
<li><a id="step2" href="#tabs-2" >step 2</a></li>
</ul>
<div id="tabs-1">
<form method='post' name='field1' id='field1' action='ureg4.php#tabs-1' >
<select name='animal' value='<?php echo $_SESSION['animal']?>'>
<option value='1' <?php echo $selected1?>>option one</option>
<option value='2' <?php echo $selected2?>>option two</option>
</select>
<input type='submit' name='submit' value='save'>
</form>
</div>
<div id="tabs-2">
<form method='post' name='field2' id='field2' action='ureg4.php#tabs-2' >
<?php
if ($_SESSION['animal']==1){echo "<input type='checkbox' name='a' value='dog' id='a'".$checked1." /><label for='a'><span class='icon'></span>Dog</label><br>";}
if ($_SESSION['animal']==2){echo "<input type='checkbox' name='z' value='horse' id='z' ".$checked2." /><label for='z'><span class='icon'></span>horse</label><br>";}
?>
<input type='submit' name='submit' value='save'>
</form>
</div>
</div>

Answer Source

Check this

session_start(); 

if (!empty($_SESSION['animal'])){
    if($_SESSION['animal']=='1'){
        $selected1='selected';
        $selected2='';
    } else {
        $selected2='selected';
        $selected1='';
    }
}

if (!empty($_POST['animal'])){

    if ($_POST['animal']=='1'){
        $selected1='selected';$selected2='';
        $_SESSION['animal']=$_POST['animal'];
    }else {
        $selected2='selected';
        $selected1='';
        $_SESSION['animal']=$_POST['animal'];
    }
}


if (!empty($_POST)){

    if ($_POST['a']=='dog'){
        $_SESSION['a']=$_POST['a'];
        $checked1='checked';
    } else {
        $checked1='';
        unset ($_SESSION['a']);
    }
}


if (!empty($_POST)){
    if ($_POST['z']=='horse'){
        $_SESSION['z']=$_POST['z']; 
        $checked2='checked';
     } else {
        $checked2='';
        unset ($_SESSION['z']);
     }
}




if($_SESSION['z']=='horse'){
   $checked2='checked';
}


if($_SESSION['a']=='dog'){
   $checked1='checked';
}