Remembering ticked check boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drummond40
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 2:21 am

Remembering ticked check boxes

Post by drummond40 »

Hi guys,

I'm having a problem with PHP and checkboxes hope someone can help.

I am outputting some customer names from a database and each of these names is outputted with a checkbox.
Names are displayed by there first letter via A-Z links.

This all works fine, but if I check some names starting with A e.g. Andrew, Adam and the click B and check Brian the previously tchecked names are unticked.

I've tried using sessions to no avail, any ideas?

Thanks in advance.
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Re: Remembering ticked check boxes

Post by Corvin »

What exactly did you try? Post your code..
drummond40
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 2:21 am

Re: Remembering ticked check boxes

Post by drummond40 »

Below is the code I have.
A working example can be found at http://www.jasongooch.co.nz/address.php?value=0
Pretty new to PHP.

Code: Select all

 
<body>
<center>
<a href="address.php?value=A">A</a> |
<a href="address.php?value=B">B</a> |
<a href="address.php?value=C">C</a> |
<a href="address.php?value=D">D</a> |
<a href="address.php?value=E">E</a> |
<a href="address.php?value=F">F</a> |
<a href="address.php?value=G">G</a> |
<a href="address.php?value=H">H</a> |
<a href="address.php?value=I">I</a> |
<a href="address.php?value=J">J</a> |
<a href="address.php?value=K">K</a> |
<a href="address.php?value=L">L</a> |
<a href="address.php?value=M">M</a> |
<a href="address.php?value=N">N</a> |
<a href="address.php?value=O">O</a> |
<a href="address.php?value=P">P</a> |
<a href="address.php?value=Q">Q</a> |
<a href="address.php?value=R">R</a> |
<a href="address.php?value=S">S</a> |
<a href="address.php?value=T">T</a> |
<a href="address.php?value=U">U</a> |
<a href="address.php?value=V">V</a> |
<a href="address.php?value=W">W</a> |
<a href="address.php?value=X">X</a> |
<a href="address.php?value=Y">Y</a> |
<a href="address.php?value=Z">Z</a> 
</center>
<?php 
$person = $_POST['name[]']; 
if(isset($_POST['name[]']));
echo "<form method=\"post\" action=\"mark.php\">"; echo "<input type=\"Submit\" value=\"Submit\">"; 
$identity = "id";
$person = "name";
// Make a MySQL Connection
mysql_connect("localhost", "???", "???") or die(mysql_error());
mysql_select_db("???") or die(mysql_error());
$result = mysql_query("SELECT $identity, $person FROM A WHERE name LIKE '$value%' ") 
or die(mysql_error());  
if(isset($_GET['value']))
{
$value = $_GET['value'];
}
else
{
$value = '';  // set a default value to be sure
}
if(isset($_GET['id']))
{
$identity = $_GET['id'];
}
//echo a table
echo "<center><table border='0'>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row["name"];
    echo "</td>";
        echo "<td>";
print "<td><form method=\"post\" action=\"mark.php\"><input type=\"checkbox\" method=\"post\" name=\"name[]\" value=\"{$row['name']}\" ></td>\n";
        echo "</td>";
    echo "</tr>";
} 
        echo "</table></center>";
        echo "</form>";
?>
</body>
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Remembering ticked check boxes

Post by Christopher »

You have a couple of options. You can use sessions to save the selected values. You said you tried that. or you can pass the selected values in every one of the A-Z links. You would add them as params each time you built the page. You also might want to generate those links in a loop.
(#10850)
drummond40
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 2:21 am

Re: Remembering ticked check boxes

Post by drummond40 »

Yeah sessions sound like the best idea I'm probably implementing my sessions wrong tho.
Can someone give me an example of how the sessions would work with my checkboxes?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Remembering ticked check boxes

Post by Christopher »

Post your code.
(#10850)
drummond40
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 2:21 am

Re: Remembering ticked check boxes

Post by drummond40 »

Address.php

session_start();
$_SESSION["name"] = "$row['name']";

mark.php

session_start();
echo $_SESSION["name"];

Yeah I know I have no idea :)
But that's what I was trying??
drummond40
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 2:21 am

Re: Remembering ticked check boxes

Post by drummond40 »

Hi guys I'm still having problems trying to store these checkbox values into a session.

Code: Select all

<input type=\"checkbox\" method=\"post\" name=\"name[]\" value=\"{$row['name']}\" >
I've been trying to store the name[] in the session thinking this will remember which box was checked on the previous page.

I can get sessions to work but not with checkboxes?
Any ideas?

Thanks for the help.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Remembering ticked check boxes

Post by Christopher »

Address.php

Code: Select all

session_start();
$_SESSION['name'] = $_POST['name'];
mark.php

Code: Select all

session_start();
echo '<pre>' . print_r($_SESSION["name"], 1) . '</pre>';
(#10850)
drummond40
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 2:21 am

Re: Remembering ticked check boxes

Post by drummond40 »

Cheers arborint that session you posted works.

But what I'm trying to do is tick the checkboxes with the names starting with A, then click B tick the names starting with B and submit to mark.php . Now all ticked names starting with A and B should be stored in the session.

But the problem is only the current pages input values are being stored in the session.
For example the names starting with B are sent but not the A names.

So I guess a better question is how do I kept the checkboxes ticked when the page changes?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Remembering ticked check boxes

Post by Christopher »

Maybe something like:

Address.php

Code: Select all

session_start();
if (isset($_SESSION['name'])) {
     $_SESSION['name'] = array_merge($_SESSION['name'], $_POST['name']);
} else {
     $_SESSION['name'] = $_POST['name'];
}
You will need to add some logic to clear previously set checkboxes.
(#10850)
Post Reply