Jump to content

Deleting Ticked Checkboxes, and it hurts


ElChoco

Recommended Posts

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

[code]<?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>
[/code]
Link to comment
Share on other sites

<?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
Link to comment
Share on other sites

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.
[code]  <?php
  if (isset($_POST["checked"]))
  {
    foreach ($_POST["checked"] as $checkbox)
    {
      echo '$checkbox';
  $sql="update searchresults set view='0' WHERE count = '$checkbox'";
      mysql_query($sql);
    }
  } 
  ?>[/code]
Do I have my array messed up?
Thanks again. This is a great forum.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.