Solved

save checkboxes in a session, keep checkall functionality

Posted on 2010-08-31
22
416 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:rgb192
  • 12
  • 10
22 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33569860
"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 http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26439503.html#a33562169) was better) and, if I understand your question, you wish keep in a session variable checked checkboxes: it's right?
0
 

Author Comment

by:rgb192
ID: 33570079
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


0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33570244
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
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33570731
I forgot: if you have a session running, checkboxes values array will be saved in $_SESSION so you can access with $_SESSION['checklist'].
0
 

Author Comment

by:rgb192
ID: 33570758
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

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33570794
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.
0
 

Author Comment

by:rgb192
ID: 33571010
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

0
 

Author Comment

by:rgb192
ID: 33571019
the checkall checkbox does not check all checkboxes

also

no change occurs
when submit button is pressed
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33571057
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']; ?>"
0
 

Author Comment

by:rgb192
ID: 33571108
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

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33571316
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

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rgb192
ID: 33571667
syntax error line 17
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33571836
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

0
 

Author Comment

by:rgb192
ID: 33572077
submits and displays checked values
and keeps the checked checkboxes checked

but the checkbox "check control"
does not check all checkboxes when checked
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33572369
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...
0
 

Author Comment

by:rgb192
ID: 33577620
yes
i used firefox3 and ie8
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33577717
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
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 33578245
Well, I found a workaround. The problem is (as usual) ie(6,7,8,9,10...) and its limited support to css and other useful things. Anyway, we have to fight against its limits and so try this code: in my system  (Windows 7) works fine both in ffx3.6.8 and in ie8. Hope this solves your problem.

Bye
<script language="javascript">
<!--        Script by hscripts.com          -->
<!--        copyright of HIOX INDIA         -->
<!-- Free javascripts @ http://www.hscripts.com -->
    checked=false;
    function checkedAll (frm1) {
        var aa= document.getElementById('frm1');
        if (checked == false){
           checked = true
        }else{
          checked = false
        }
	for (var i =0; i < aa.elements.length; i++){
            aa.elements[i].checked = checked;
	}
    }
<!-- Script by hscripts.com -->
</script>
<?php
$check_list = array();
if (isset($_POST['check_list'])){
  foreach ($_POST['check_list'] as $chk){
    $check_list[] = $chk;
  }
}
echo "the following values have been checked ";
echo "<form id='frm1' 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' 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>";
}

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

echo "<input type='checkbox' name='Check_ctr' value='yes' onClick='checkedAll(frm1);'><b>Check Control</b><br>";
echo "<input type='submit'>";
echo "</form>";
?>

Open in new window

0
 

Author Comment

by:rgb192
ID: 33579793
when  you test it on your machine
does it display
the following areas have been checked
and say what they are
0
 

Author Closing Comment

by:rgb192
ID: 33580014
works great

thank you for all your help
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33580045
Hi, rgb192, I was writing to you when email with points has arrived. Thank you for points and good luck with your project.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 33580092
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

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now