Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

save checkboxes in a session, keep checkall functionality

store the current checkboxes in a session

so can print checkbox1 is checked..

must be saved in a session

note:
this code has a checkbox ('check all'/'uncheck all') feature
in serverside javascript

I would like to have/keep this functionality
<SCRIPT LANGUAGE="JavaScript">
<!-- 

<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

// End -->
</script>


echo "the following values have been checked"; 
<form name="myform" action="#" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list" value="1">ASP<br>
<input type="checkbox" name="check_list" value="2">PHP<br>
<input type="checkbox" name="check_list" value="3">JavaScript<br>
<input type="checkbox" name="check_list" value="4">HTML<br>
<input type="checkbox" name="check_list" value="5">MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Check Control</b><br> 
<input type="submit">
</form>
<?
?>

Open in new window

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

"serverside javascript"? This doesn't exist: javascript is a client-side scripting language, PHP a server-side scriptin language. You have a javascript function (but the one posted by leakim971 in the post ID:33563412 at https://www.experts-exchange.com/questions/26439503/checkbox-'all'.html?anchorAnswerId=33562169#a33562169) was better) and, if I understand your question, you wish keep in a session variable checked checkboxes: it's right?
Avatar of rgb192

ASKER

you are right: I meant to say 'client side for javascript'

leakim971 answer was in javascript
that question did not include a session

>>and, if I understand your question, you wish keep in a session variable checked checkboxes: it's right?
I would like this done in php, if possible


Of course. To do it you have to change your checkboxes name like this:

<?php
echo<<<END
the following values have been checked;
<form name="myform" action="#" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list[]" value="1">ASP<br>
<input type="checkbox" name="check_list[]" value="2">PHP<br>
<input type="checkbox" name="check_list[]" value="3">JavaScript<br>
<input type="checkbox" name="check_list[]" value="4">HTML<br>
<input type="checkbox" name="check_list[]" value="5">MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Check Control</b><br>
<input type="submit">
</form>
END;
?>

I used <<< syntax for brevity, but you can use normal "" notation. What it's important is that you give to each checkbox a name terminated with brackets: checklist[]. This create an array wich is passed when form is submitted.
Since only checked value are submitted with checkboxes and radios, in the page that receive the post (I presume the same page) you can write something like:

<?php
if (isset($_POST['checklist'])){
  foreach ($_POST['cheklist'] as $chk){
    $checklist[] = $chk;
  }
}
?>

This way you recreate your array with checkboxes checked values. So printing the form you can check if each checkbox is in array:

if (in_array('1', $checklist)){
  echo "<input type=\"checkbox\" checked=\"checked\" name=\"check_list\" value=\"1\">ASP<br>";
}else {
  echo "<input type=\"checkbox\" name=\"check_list\" value=\"1\">ASP<br>";
}

Hope this is clear: feel free to ask if it is not.

Best regards
I forgot: if you have a session running, checkboxes values array will be saved in $_SESSION so you can access with $_SESSION['checklist'].
Avatar of rgb192

ASKER

form does not submit
Warning: in_array() [function.in-array]: Wrong datatype for second argument in  on line 42
<SCRIPT LANGUAGE="JavaScript">
<!-- 

<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

// End -->
</script>





<?php
if (isset($_POST['checklist'])){
  foreach ($_POST['cheklist'] as $chk){
    $checklist[] = $chk; 
  }
}
?>




echo<<<END
the following values have been checked; 
<form name="myform" action="#" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list[]" value="1"

<?
if (in_array('1', $checklist)){
  echo "<input type=\"checkbox\" checked=\"checked\" name=\"check_list\" value=\"1\">ASP<br>";
}else {
  echo "<input type=\"checkbox\" name=\"check_list\" value=\"1\">ASP<br>";
}
?>
>ASP<br>
<input type="checkbox" name="check_list[]" value="2">PHP<br>
<input type="checkbox" name="check_list[]" value="3">JavaScript<br>
<input type="checkbox" name="check_list[]" value="4">HTML<br>
<input type="checkbox" name="check_list[]" value="5">MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Check Control</b><br> 
<input type="submit">
</form>
END;

Open in new window

Sorry, try this

<?php
$checklist = array();
if (isset($_POST['checklist'])){
  foreach ($_POST['cheklist'] as $chk){
    $checklist[] = $chk;
  }
}
?>

and then

<?

if (count($checklist)>0 && in_array('1', $checklist)){
  echo "<input type=\"checkbox\" checked=\"checked\" name=\"check_list\" value=\"1\">ASP<br>";
}else {
  echo "<input type=\"checkbox\" name=\"check_list\" value=\"1\">ASP<br>";
}
?>

My previous code didn't allow to the page to display properly before form was submitted: I'm sorry.
Avatar of rgb192

ASKER

the checkall checkbox does not check all checkboxes

also

no change occurs
when input button is pressed
<SCRIPT LANGUAGE="JavaScript">
<!-- 

<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

// End -->
</script>





<?php
$checklist = array();
if (isset($_POST['checklist'])){
  foreach ($_POST['cheklist'] as $chk){
    $checklist[] = $chk; 
  }
}
?>





echo<<<END
the following values have been checked; 
<form name="myform" action="#" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list[]" value="1"

<?

if (count($checklist)>0 && in_array('1', $checklist)){
  echo "<input type=\"checkbox\" checked=\"checked\" name=\"check_list\" value=\"1\">ASP<br>";
}else {
  echo "<input type=\"checkbox\" name=\"check_list\" value=\"1\">ASP<br>";
}
?>

>ASP<br>
<input type="checkbox" name="check_list[]" value="2">PHP<br>
<input type="checkbox" name="check_list[]" value="3">JavaScript<br>
<input type="checkbox" name="check_list[]" value="4">HTML<br>
<input type="checkbox" name="check_list[]" value="5">MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Check Control</b><br> 
<input type="submit">
</form>
END;

Open in new window

Avatar of rgb192

ASKER

the checkall checkbox does not check all checkboxes

also

no change occurs
when submit button is pressed
Since your form action is null ("#") form is not submitted (I've not noticed this before). You should set an action for your form. If you wish the actual page is reloaded with new values selected try to set action this way

action="<?php echo $_SERVER['PHP_SELF']; ?>"
Avatar of rgb192

ASKER

the checkall checkbox does not check all checkboxes

also

no change occurs
when submit button is pressed
<SCRIPT LANGUAGE="JavaScript">
<!-- 

<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

// End -->
</script>





<?php
$checklist = array();
if (isset($_POST['checklist'])){
  foreach ($_POST['cheklist'] as $chk){
    $checklist[] = $chk; 
  }
}
?>





echo<<<END
the following values have been checked; 
<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<b>Scripts for Web design and programming</b><br>
<input type="checkbox" name="check_list[]" value="1"

<?

if (count($checklist)>0 && in_array('1', $checklist)){
  echo "<input type=\"checkbox\" checked=\"checked\" name=\"check_list\" value=\"1\">ASP<br>";
}else {
  echo "<input type=\"checkbox\" name=\"check_list\" value=\"1\">ASP<br>";
}
?>

>ASP<br>
<input type="checkbox" name="check_list[]" value="2">PHP<br>
<input type="checkbox" name="check_list[]" value="3">JavaScript<br>
<input type="checkbox" name="check_list[]" value="4">HTML<br>
<input type="checkbox" name="check_list[]" value="5">MySQL<br>

<input type="checkbox" name="Check_ctr" value="yes"
onClick="Check(document.myform.check_list)"><b>Check Control</b><br> 
<input type="submit">
</form>
END;

Open in new window

Well, I revisited entirely your code and I've rewritten it using leakin971 function: since we have chenged checkbox names, your function couldn't work bnecause based on checkbox name. Leakim971 function instead is based on a class attribute so we can rename checkboxes as we wish (or better: as we need). Notice that if you wish use <<< syntax you have to do ite within php tag. However, below you find the entire code rewritten: simply copy and paste it to test.

Bye

<script language="javascript">
    function setCheckboxes(groupName, state) {
        var inputs = document.getElementsByTagName("input");
        for(var i=0;i<inputs.length;i++) {
            if(inputs[i].getAttribute("class") && inputs[i].getAttribute("class").indexOf("group1")>=0) {
                inputs[i].checked = state;
            }
        }
    }
</script>





<?php
$checklist = array();
if (isset($_POST['checklist'])){
  foreach ($_POST['cheklist'] as $chk){
    $checklist[] = $chk; 
  }
}





echo "the following values have been checked ";
echo "<form name='myform' action='".$_SERVER['PHP_SELF']."' method='post'>";
echo "<b>Scripts for Web design and programming</b><br>';";
echo "<input type='checkbox' name='check_list[]' value='1';";

if (count($checklist)>0 && in_array('1', $checklist)){
  echo '<input type=\'checkbox\' checked=\'checked\' name=\'check_list\' class=\'group1\' value=\'1\'>ASP<br>';
}else{
  echo '<input type=\'checkbox\' name=\'check_list\' class=\'group1\' value=\'1\'>ASP<br>';
}

echo "ASP<br>";
echo "<input type='checkbox' name='check_list[]' class='group1' value='2'>PHP<br>";
echo "<input type='checkbox' name='check_list[]' class='group1' value='3'>JavaScript<br>";
echo "<input type='checkbox' name='check_list[]' class='group1' value='4'>HTML<br>";
echo "<input type='checkbox' name='check_list[]' class='group1' value='5'>MySQL<br>";

echo "<input type='checkbox' name='Check_ctr' value='yes' onClick='setCheckboxes('group1', this.checked);'><b>Check Control</b><br>"; 
echo "<input type='submit'>";
echo "</form>";
?>

Open in new window

Avatar of rgb192

ASKER

syntax error line 17
In my browser I didn't receive any error, but code didn't work and I've corrected it. If it gives you an error, please tell me about in detail and tomorrow I'll take a look. Try this new coe, please.
<script language="javascript">
    function setCheckboxes(groupName, state) {
        var inputs = document.getElementsByTagName("input");
        for(var i=0;i<inputs.length;i++) {
            if(inputs[i].getAttribute("class") && inputs[i].getAttribute("class")=="group1") {
                inputs[i].checked = state;
            }
        }
    }
</script>





<?php
$check_list = array();
if (isset($_POST['check_list'])){
  foreach ($_POST['check_list'] as $chk){
    $check_list[] = $chk;
    echo $chk."<br>";
  }
}





echo "the following values have been checked ";
echo "<form name='myform' action='".$_SERVER['PHP_SELF']."' method='post'>";
echo "<b>Scripts for Web design and programming</b><br>";

if (count($check_list)>0 && in_array('1', $check_list)){
  echo "<input type='checkbox' checked='checked' name='check_list[]' class='group1' value='1' />ASP<br>";
}else{
  echo "<input type='checkbox' name='check_list[]' class='group1' value='1'>ASP<br>";
}

echo "ASP<br>";
echo "<input type='checkbox' name='check_list[]' class='group1' value='2'>PHP<br>";
echo "<input type='checkbox' name='check_list[]' class='group1' value='3'>JavaScript<br>";
echo "<input type='checkbox' name='check_list[]' class='group1' value='4'>HTML<br>";
echo "<input type='checkbox' name='check_list[]' class='group1' value='5'>MySQL<br>";

echo "<input type='checkbox' name='Check_ctr' value='yes' onClick='setCheckboxes(\"group1\", this.checked);'><b>Check Control</b><br>";
echo "<input type='submit'>";
echo "</form>";
?>

Open in new window

Avatar of rgb192

ASKER

submits and displays checked values
and keeps the checked checkboxes checked

but the checkbox "check control"
does not check all checkboxes when checked
In my test all works fine. Are you sure to have copied and pasted exactly as in my previous post? I made changes in onclick even also...
Avatar of rgb192

ASKER

yes
i used firefox3 and ie8
in firefox 3.6.8 it works (for me) but in the "beloved" ie it doesn't work. Now I go to test something for ie, but it is strange that it doesn't work in your ffx!

Write you later
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

when  you test it on your machine
does it display
the following areas have been checked
and say what they are
Avatar of rgb192

ASKER

works great

thank you for all your help
Hi, rgb192, I was writing to you when email with points has arrived. Thank you for points and good luck with your project.
I post the code modified for the checkboxes (I have yet written it). I've deleted id and class attribute since they are not needed with the new javascript code. Bye
if (count($check_list)>0){
    if (in_array('1', $check_list)){
        echo "<input type='checkbox' checked='checked' id='check' name='check_list[]' class='group1' value='1' />ASP<br>";
    }else{
        echo "<input type='checkbox' id='check' name='check_list[]' class='group1' value='1'>ASP<br>";
    }
    if (in_array('2', $check_list)){
        echo "<input type='checkbox' checked='checked' name='check_list[]' value='2' />PHP<br>";
    }else{
        echo "<input type='checkbox' name='check_list[]' value='2'>PHP<br>";
    }
    if (in_array('3', $check_list)){
        echo "<input type='checkbox' checked='checked' name='check_list[]' value='3' />Javascript<br>";
    }else{
        echo "<input type='checkbox' name='check_list[]' value='3'>Javascript<br>";
    }
    if (in_array('4', $check_list)){
        echo "<input type='checkbox' checked='checked' name='check_list[]' value='4' />HTML<br>";
    }else{
        echo "<input type='checkbox' name='check_list[]' value='4'>HTML<br>";
    }
    if (in_array('5', $check_list)){
        echo "<input type='checkbox' checked='checked'name='check_list[]' value='5' />MySQL<br>";
    }else{
        echo "<input type='checkbox' name='check_list[]' value='5'>MySQL<br>";
    }
}

Open in new window