Jump to content


Photo

Deleting Ticked Checkboxes, and it hurts


  • Please log in to reply
4 replies to this topic

#1 ElChoco

ElChoco
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 August 2006 - 12:33 PM

I would like to 'hide' selections based on ticked checkboxes. I'm running into a bit of trouble though. I have the select all, de-select all buttons working but when I try to change the table for all those clicked it fails.  ??? It hurts my brains, all of them. Thanks

<?PHP
session_start();	// This connects to the existing session

$username = $_SESSION['username'];
$password = $_SESSION['password'];
if (!isset($username) || !isset($password)) 
{
    header( "Location: http://www.bedwettingstore.com/login.php?login=fail" );
}
/*********************************
** The Welcome Table and logout **
*********************************/
?> 

<table width="100%"><tr><td align="left">Welcome <font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b><?=$username?></b></font></td><td align="right"><a href="logout.php">Log Out</a></td></tr></table><br /><br />

<?PHP
/******************
** DB Variables  **
******************/
$dbhost = "###.###.###.###"; 
$dbuser = "username"; 
$dbpass = "password"; 
$dbname = "dbname"; 

/**********************
**   Connect to DB   **
**********************/ 
global $link;

$link = mysql_connect("$dbhost","$dbuser","$dbpass"); 
mysql_select_db("$dbname",$link)or die( "Unable to select database".mysql_error()); 

// Get All Fields from table
$query="SELECT * FROM searchresults where view=\"1\"";
$result=mysql_query($query);

// Count number of (different) data
$num = mysql_numrows($result);

//Close Connection
//mysql_close();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script type="text/javascript" language="javascript">
/*********************************/
/*** CHECK or UnCHECK all boxes **/
/*********************************/
<!-- 
var form='checkboxForm' //Give the form name here

function SetChecked(val,chkName) {
dml=document.forms[form];
len = dml.elements.length;
var k=0;
for( k=0 ; k<len ; k++) {
if (dml.elements[k].name==chkName) {
dml.elements[k].checked=val;
}
}
}

function deleteRecords(checkbox)
{
 <?php 
   if (isset($_POST["checked"]))
   {
     foreach ($_POST["checked"] as $checkbox)
	 {
       $sql="update searchresults set view='0'";
       mysql_query($sql);
     }
  }   
  ?>

}


// -->
</script>

</head>

<body>

<a href="javascript:SetChecked(1, 'checked[]')">Check All</a> &nbsp; <a href="javascript:SetChecked(0, 'checked[]')">Un-Check All</a> 
 &nbsp; <a href="javascript:deleteRecords('checked[]')">Delete Records</a>
<form name="checkboxForm" method="post" action="#" onSubmit="return ValidateForm(this, 'checked')">
<table border="1"><tr><td align="center">&nbsp;</td><td align="center" width="12">#</td><td align="center">Date & Time</td><td align="center">Search Query</td><td align="center">Page</td></tr>

<?
$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"count");
$datetime=mysql_result($result,$i,"datetime");
$search=mysql_result($result,$i,"search");
$page=mysql_result($result,$i,"page");
?>
<tr><td><input type="checkbox" name="checked[]" value="<?=$id?>" > <td align=\"center\"><?=$id?></td><td align=\"center\"><?=$datetime?></td><td align="center"><?=$search?></td><td align="center"><?=$page?></td></tr>
<?PHP
$i++;
}
?>
</table>

</form>
</body>
</html>


#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 11 August 2006 - 01:01 PM

<?php
  if (isset($_POST["checked"]))
  {
    foreach ($_POST["checked"] as $checkbox)
{
      $sql="update searchresults set view='0'";
      mysql_query($sql);
    }
  } 
  ?>

needs to be

<?php
  if (isset($_POST["checked"]))
  {
    foreach ($_POST["checked"] as $checkbox)
{
      $sql="update searchresults set view='0' WHERE count = '$checkbox'";
      mysql_query($sql);
    }
  } 
  ?>


because otherwise u will be setting the whole table to view=0

Regards
liam

www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 ElChoco

ElChoco
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 August 2006 - 01:12 PM

Oops, yeah. Thanks.
What I know, is that the function is being called, but $checkbox isn't set correctly. If I put an echo in there nothing prints.
  <?php
   if (isset($_POST["checked"]))
   {
     foreach ($_POST["checked"] as $checkbox)
    {
       echo '$checkbox';
	   $sql="update searchresults set view='0' WHERE count = '$checkbox'";
       mysql_query($sql);
     }
  }   
  ?>
Do I have my array messed up?
Thanks again. This is a great forum.



#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 11 August 2006 - 01:31 PM

just looked at your code again and are you trying to set a javascript function to do php? if so then you can't as the php is parsed as soon as u access the site and wont be stored in any clientside language..

Hope you understand this

Regards
liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 ElChoco

ElChoco
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 August 2006 - 02:07 PM

OH! well duh, I feel just stupid.  ::)
I've been looking at this for much too long to not have noticed that. Thank you.
Everything works now, it submits to another page.

I do love this forum! ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users