Page 1 of 1

Checked Boxes in PHP form

Posted: Mon Nov 29, 2010 12:26 am
by robynprivette
I have a form that allows users to pull up info and check what they want added to the site. How can I change my form so that when a user comes back to the site and enters the info, what has already been added to the DB is checked and what hasn't been added is not checked? Currently when they come back to the site all boxes are left unchecked. Here is my code.

ScrollRead.php

Code: Select all

</script>


<script type='text/javascript'>
checked=false;
function checkAll () {
var aa= document.getElementById('lomp');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++)
{
aa.elements[i].checked = checked;
}
}
</script>



<table border=1px>
<tr>
<th>Image</th>
<th>id</th>
<th>stolen/bred on</th>
<th>parents</th>
<th>gender</th>
<th>type</th>
<th>clicks</th>
<th>views</th>
<th>unique views</th>
<th>hours left</th>
<th>add dragon</th></tr>
<form name="form1" id="lomp" method="get" action="ScrollReadGet.php"> 
<?php
ini_set("display_errors",false);
$fdd = $_POST['user'];
$user = str_replace(" ","%20","$fdd");
echo ''.$fdd.'';
echo '<input type=hidden value="'.$user.'" name=user><br>';
$young = unserialize(file_get_contents('http://dragcave.net/api/xxxx/serialize/user_young/'.$user.''));

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

foreach($young['errors'] as $error) {
  if($error[0] == 4) {echo '<center>Sorry, this user has no dragons.</center>'; return;}
}


foreach ($young['dragons'] as $key => $value) {
$data = unserialize(file_get_contents('http://dragcave.net/api/xxxx/serialize/view/'.$key));
  echo '
<tr><td><a href="http://www.dragcave.net/view/'.$key.'"><img src="http://www.dragcave.net/image/'.$key.gif.'" border="0"></a></td>';
$data = array_pop($data['dragons']);		
if($data['start'] == "0") echo "{$data['id']} is hidden!";
		elseif($data['hoursleft'] == -2) echo "<td>{$data['id']} is dead!</td>";
		else {
			if($data['grow'] != 0) $type = "adult";
			elseif($data['hatch'] != "0" && $data['hoursleft'] == -1) $type="frozen hatchling";
			elseif($data['hatch'] != "0") $type="hatchling";
			else $type="egg";
  echo "<td>$key</td>";
  echo "<td>{$data['start']}</td>";
  echo "<td>Mother- <a href=\"http://www.dragcave.net/view/{$data['parent_f']}\" target=\"blank\">{$data['parent_f']}</a><br>Father- <a href=\"http://www.dragcave.net/view/{$data['parent_m']}\" target=\"blank\">{$data['parent_m']}</a></td>";
  echo "<td>{$data['gender']}</td>";
  echo "<td>{$type}</td>";
  echo "<td>{$data['clicks']}</td";
  echo "<td>{$data['views']}</td>";
  echo "<td>{$data['unique']}</td>";
  echo "<td>{$data['hoursleft']} hours</td>";
  echo '<td><input type="checkbox" name="checkboxes[]" value="'.$key.'" border="0"></a></td>';
}
foreach ($data['clicks'] as $help => $me) {
  echo '$help';
}
}


?>
</tr>
<td colspan="11"<input type='checkbox' name='checkz' onClick='checkAll();'>Check all/Uncheck all<br>
<input type="submit" name="Submit" class="button" value="Add Dragons"></td>
</tr>
</table>
ScrollReadGet.php

Code: Select all


<table>
<tr><td>
<?php
ini_set("display_errors",false);
$link = mysql_connect('xxxx', 'xxxx', 'xxxx',true); if (!$link) {      die('Could not connect: ' . mysql_error()); }
mysql_select_db("thehatch_dragon") or die(mysql_error());
$ids=$_GET['checkboxes'];

$user = $_GET['user'];
$young = unserialize(file_get_contents('http://dragcave.net/api/xxxx/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. --<br> ';
 echo 'Dragons now entered:<br>';
foreach ($ids as $hey) {
echo '<a href="http://www.dragcave.net/view/'.$hey.'" target="frame1"><img src="http://www.dragcave.net/image/'.$hey.'.gif" border="0"></a></&nbsp;>';

$data = unserialize(file_get_contents('http://dragcave.net/api/xxxx/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.<br>';
}
else
{
if  (! $hatch) {
$query4 = "INSERT INTO egg(code, user) VALUES('$hey', '$user')";
mysql_query($query4);
echo '=>in  Nest.<br>';
}
else
{
$query2 = "INSERT INTO hatch(code, user) VALUES('$hey', '$user')";
mysql_query($query2);
echo '=>in  Nursery.<br>';
}
}
}
?>
</td></tr>
</table>

<= <a href="main.php" target="mainview">Back</a>

Re: Checked Boxes in PHP form

Posted: Mon Nov 29, 2010 4:08 am
by social_experiment
Is the form open to any user or reserved for registered users?

Re: Checked Boxes in PHP form

Posted: Mon Nov 29, 2010 4:24 am
by robynprivette
Any user...

Re: Checked Boxes in PHP form

Posted: Mon Nov 29, 2010 4:56 am
by social_experiment

Code: Select all

<?php
 // connect to the server, select
 // the database.

 // example 1
 $query = "SELECT value FROM table WHERE checked = 'Y'";
 $sql = mysql_query($query);

 while ($sql_array = mysql_fetch_array($sql)) {
  // create your checkboxes of values that have 
  // been selected
  // note the 'checked="checked"' property.
  echo '<input type="checkbox" name="'. $sql_array['value'] .'" 
  value="'. $sql_array['value'] .'" checked="checked" />';
 }

 // example 2
 $query_u = "SELECT value FROM table WHERE checked = 'N'";
 $sql_u = mysql_query($query_u);

 while ($sql_array = mysql_fetch_array($sql_u)) {
  // create your checkboxes of values that have 
  // not been selected
  echo '<input type="checkbox" name="'. $sql_array['value'] .'" 
  value="'. $sql_array['value'] .'" />';
 }

?>
You have to select (from the database) the values marked as 'checked' (example 1) and the values marked 'un-checked' (example 2). The while statements creates the checkboxes. The while statements in both of the examples looks the same except for the "checked" property in the first example. This determines whether a checkbox is displayed as ticked or not. Your code should be placed within form tags if you want users to submit the information.

Code: Select all

<?php
 echo '<form>'; 
 // code to generate checkboxes 
 echo '</form>';
?>

Re: Checked Boxes in PHP form

Posted: Mon Nov 29, 2010 5:40 am
by robynprivette
forgive me if i seem naive but how do i implement this with what i already have? and thank you for your help :)

Re: Checked Boxes in PHP form

Posted: Mon Nov 29, 2010 6:21 am
by social_experiment
The two pages that you pasted, do you want the checkboxes to appear on one of them?

Re: Checked Boxes in PHP form

Posted: Mon Nov 29, 2010 6:12 pm
by robynprivette
Yes they will appear on the ScrollRead.php page. I have a form I just didn't post it...

scroll.php

Code: Select all

<form name="input" action="ScrollRead.php" target="mainview" method="post">
Scroll Name:
<input type="text" name="user" maxlength="40" size="15"/>
<input type="submit" value="Read Scroll" class="button" />
</form>

Re: Checked Boxes in PHP form

Posted: Tue Nov 30, 2010 12:17 am
by social_experiment

Code: Select all

<form name="input" action="ScrollRead.php" target="mainview" method="post">
Scroll Name:
<input type="text" name="user" maxlength="40" size="15"/>
<?php 
 // connect to the server, select
 // the database.

 $query = "SELECT value FROM table WHERE 
 checked = 'Y'";
 $sql = mysql_query($query);

 while ($sql_array = mysql_fetch_array($sql)) {
  // create your checkboxes of values that have 
  // been selected
  echo '<input type="checkbox" name="'. $sql_array['value'] .'" 
  value="'. $sql_array['value'] .'" checked="checked" />';
 }

 $query_u = "SELECT value FROM table WHERE 
 checked = 'N'";

 while ($sql_array = mysql_fetch_array($sql)) {
  // create your checkboxes of values that have 
  // been selected
  echo '<input type="checkbox" name="'. $sql_array['value'] .'" 
  value="'. $sql_array['value'] .'" />';
 }
?>
<input type="submit" value="Read Scroll" class="button" />
</form>
The checkboxes are created between the form tags, just after the 'user' text input field.

Re: Checked Boxes in PHP form

Posted: Tue Nov 30, 2010 12:46 am
by robynprivette
that doesn't work. maybe i'm filling it in wrong...

Re: Checked Boxes in PHP form

Posted: Tue Nov 30, 2010 1:11 am
by social_experiment
Could you please paste your php code that will create the checkboxes?

Re: Checked Boxes in PHP form

Posted: Tue Nov 30, 2010 2:23 am
by robynprivette
this is the script that has the checkboxes listed.

Code: Select all

</script>


<script type='text/javascript'>
checked=false;
function checkAll () {
var aa= document.getElementById('lomp');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++)
{
aa.elements[i].checked = checked;
}
}
</script>



<table border=1px>
<tr>
<th>Image</th>
<th>id</th>
<th>stolen/bred on</th>
<th>parents</th>
<th>gender</th>
<th>type</th>
<th>clicks</th>
<th>views</th>
<th>unique views</th>
<th>hours left</th>
<th>add dragon</th></tr>
<form name="form1" id="lomp" method="get" action="ScrollReadGet.php">
<?php
ini_set("display_errors",false);
$fdd = $_POST['user'];
$user = str_replace(" ","%20","$fdd");
echo ''.$fdd.'';
echo '<input type=hidden value="'.$user.'" name=user><br>';
$young = unserialize(file_get_contents('http://dragcave.net/api/xxxx/serialize/user_young/'.$user.''));

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

foreach($young['errors'] as $error) {
  if($error[0] == 4) {echo '<center>Sorry, this user has no dragons.</center>'; return;}
}


foreach ($young['dragons'] as $key => $value) {
$data = unserialize(file_get_contents('http://dragcave.net/api/xxxx/serialize/view/'.$key));
  echo '
<tr><td><a href="http://www.dragcave.net/view/'.$key.'"><img src="http://www.dragcave.net/image/'.$key.gif.'" border="0"></a></td>';
$data = array_pop($data['dragons']);           
if($data['start'] == "0") echo "{$data['id']} is hidden!";
                elseif($data['hoursleft'] == -2) echo "<td>{$data['id']} is dead!</td>";
                else {
                        if($data['grow'] != 0) $type = "adult";
                        elseif($data['hatch'] != "0" && $data['hoursleft'] == -1) $type="frozen hatchling";
                        elseif($data['hatch'] != "0") $type="hatchling";
                        else $type="egg";
  echo "<td>$key</td>";
  echo "<td>{$data['start']}</td>";
  echo "<td>Mother- <a href=\"http://www.dragcave.net/view/{$data['parent_f']}\" target=\"blank\">{$data['parent_f']}</a><br>Father- <a href=\"http://www.dragcave.net/view/{$data['parent_m']}\" target=\"blank\">{$data['parent_m']}</a></td>";
  echo "<td>{$data['gender']}</td>";
  echo "<td>{$type}</td>";
  echo "<td>{$data['clicks']}</td";
  echo "<td>{$data['views']}</td>";
  echo "<td>{$data['unique']}</td>";
  echo "<td>{$data['hoursleft']} hours</td>";
  echo '<td><input type="checkbox" name="checkboxes[]" value="'.$key.'" border="0"></a></td>';
}
foreach ($data['clicks'] as $help => $me) {
  echo '$help';
}
}


?>
</tr>
<td colspan="11"<input type='checkbox' name='checkz' onClick='checkAll();'>Check all/Uncheck all<br>
<input type="submit" name="Submit" class="button" value="Add Dragons"></td>
</tr>
</table>
A user submits their scrollname into the form and it lists what is on their scroll with checkboxes next to each one

Re: Checked Boxes in PHP form

Posted: Tue Nov 30, 2010 3:51 am
by social_experiment

Code: Select all

<?php
 // other code
 echo '<td><input type="checkbox" name="checkboxes[]" value="'.$key.'" border="0"></a></td>';
 }
  // checkboxes here?
  foreach ($data['clicks'] as $help => $me) {
   echo '$help';
 }
 // other code
?>
I might be wrong but i think this is where you want the checkboxes. The code (SQL queries) would go where the foreach() statement is, or inside it. What is the purpose of $data['clicks'] and what information does it hold?