Display items already submitted as checked

How can I make it so that it remembers what options the user has checked? (like when they come back to add something, what they’ve already added is already checked)

[php]

body { background: transparent; color:#000; font: normal small Verdana, Helvetica, sans-serif; font-size: 8px; } table { margin:auto; background: transparent; color:#000; width:100%; height:100; border:1px solid #B0C3D1; font: normal small Verdana, Helvetica, sans-serif; font-size: 8px; cellpadding:0px; cellspacing:0px; text-align: center; } .input { border: 1px solid #006; background: #ffc; } .input:hover { border: 1px solid #B0C3D1; background: #ff6; } .button { font-size: 10px; border: 1px solid #B0C3D1; background: #ccf; } .button:hover { font-size: 10px; border: 1px solid #B0C3D1; background: #eef; } label { display: block; width: 150px; float: left; margin: 2px 4px 6px 4px; text-align: right; } br { clear: left; } <?php ini_set("display_errors",false); $fdd = $_POST['user']; $user = str_replace(" ","%20","$fdd"); echo ''.$fdd.''; echo '
'; $young = unserialize(file_get_contents('http://dragcave.net/api/thehatch2010/serialize/user_young/'.$user.''));

foreach($young[‘errors’] as $error) {
if($error[0] == 3) {echo ‘Sorry, we were unable to locate that scroll.’; return;}
}

foreach($young[‘errors’] as $error) {
if($error[0] == 4) {echo ‘Sorry, this user has no dragons.’; return;}
}

foreach ($young[‘dragons’] as $key => $value) {
$data = unserialize(file_get_contents(‘http://dragcave.net/api/thehatch2010/serialize/view/’.$key));
echo ’

'; echo ''; } foreach ($data['clicks'] as $help => $me) { echo '$help'; }

?>

<td colspan="2"Check all/Uncheck all
Image add dragon
[/php]

You can save user choices to a cookie, and then read cookie to check previously selected items. Here is how you can do this:

In your ScrollReadGet.php file, where you process the submitted form, add this code:
[php]
$s_dragons = ‘’;
if($_GET[‘Submit’] and is_array($_GET[‘checkboxes’])){
foreach($_GET[‘checkboxes’] as $val){
$s_dragons.=($s_dragons==’’?’’:’,’).$val;
}
}
setcookie(‘dragonsadded’,$s_dragons,time()+360024365,"/"); // keep cookie for 1 year from now
[/php]

Then, in your code (what you posted here) make these changes:

your current code:
[php]foreach ($young[‘dragons’] as $key => $value) {
$data = unserialize(file_get_contents(‘http://dragcave.net/api/thehatch2010/serialize/view/’.$key));
echo ’

'; echo ' '; }[/php]

change to this:
[php]$s_dragons = $_COOKIE[‘dragonsadded’];
$arr_dragons = array();
if($s_dragons!=’’) {
$arr = explode(’,’,$s_dragons);
foreach($arr as $val){
$arr_dragons[$val]=’ checked’;
}
}
foreach ($young[‘dragons’] as $key => $value) {
$data = unserialize(file_get_contents(‘http://dragcave.net/api/thehatch2010/serialize/view/’.$key));
echo ’

'; echo ' '; }[/php]

Where exactly do I put this? B.c I put it at the end and it didn’t work.

I didn’t see an edit button… but I meant the part that goes in ScrollReadGet.php

Here is ScrollReadGet.php

[php]<?php
ini_set(“display_errors”,false);
$link = mysql_connect(’—’, ‘—’, ‘—’,true); if (!$link) { die('Could not connect: ’ . mysql_error()); }
mysql_select_db("—") or die(mysql_error());
$ids=$_GET[‘checkboxes’];

$user = $_GET[‘user’];
$young = unserialize(file_get_contents(‘http://dragcave.net/api/thehatch2010/serialize/user/’.$user.’’));

foreach ($young[‘dragons’] as $key => $value) {
$querya = “DELETE FROM hatch WHERE code=’$key’”;
$queryb = “DELETE FROM er WHERE code=’$key’”;
$queryc = “DELETE FROM egg WHERE code=’$key’”;
mysql_query($querya);
mysql_query($queryb);
mysql_query($queryc);
}
echo 'Dragons updated. –
';
echo ‘Dragons now entered:
’;
foreach ($ids as $hey) {
echo ‘</ >’;

$data = unserialize(file_get_contents(‘http://dragcave.net/api/thehatch2010/serialize/view/’.$hey.’’));
$hrsleft = $data[‘dragons’][$hey][‘hoursleft’];
$hatch = $data[‘dragons’][$hey][‘hatch’];

if ($hrsleft < 96) {
$query3 = “INSERT INTO er(code, user) VALUES(’$hey’, ‘$user’)”;
mysql_query($query3);
echo ‘=>in ER.
’;
}
else
{
if (! $hatch) {
$query4 = “INSERT INTO egg(code, user) VALUES(’$hey’, ‘$user’)”;
mysql_query($query4);
echo ‘=>in Nest.
’;
}
else
{
$query2 = “INSERT INTO hatch(code, user) VALUES(’$hey’, ‘$user’)”;
mysql_query($query2);
echo ‘=>in Nursery.
’;

}
}
}
?>[/php]

Add this anywhere at top of ScrollReadGet.php, say below this line:
[php]$ids=$_GET[‘checkboxes’];[/php]

Ok I did that and it still isn’t remembering which ones I checked ):

Try to check if cookie is saved properly. This will help you to debug.

How do I do that? :-[

You can see cookie in your browser. For FireFox, go to Tools -> Options -> Privacy -> Show Cookies
Then type in a text field your domain name, and you will see cookie name (in your case ‘dragonsadded’). Click on this row, and you will see content of this cookie file.

Nothing came up for the domain name :’(

Ok. Continue debugging… Print the value what you are saving to cookie, to do so add this line:
[php]echo $s_dragons;[/php]

just above this line in your code:
[php]setcookie(‘dragonsadded’,$s_dragons,time()+360024365,"/"); // keep cookie for 1 year from now[/php]

O.o

vSgE,Rh9G,1NMZ,XTnc
Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb25c/b1253/ipg.thehatch/ScrollReadGet.php:1) in /hermes/bosweb25c/b1253/ipg.thehatch/ScrollReadGet.php on line 51

Well, since you are debugging you can put die(); after the echo. But looks like your checkbox selection did not submit properly. Did you check any checkboxes during test?

Yep sure did. And the entries were entered correctly into the db

So if you add this code (before the line where you are sending cookie):
[php]echo $s_dragons;
die();[/php]

do you see codes of selected checkboxes? You may need to look at html source code.

Yes I get this
nZFk,vSgE,Rh9G,1NMZ,XTnc
which is the code of each box i checked

Well, so data is submitted correctly… not sure why cookies are not saved in your browser. You need to check if they are turned On in your browser.
Also try to search cookie by cookie name: dragonsadded.

You’re right. Members of my site are saying it’s working for them. I don’t know how to fix it on my end though ):

Sponsor our Newsletter | Privacy Policy | Terms of Service