Jump to content

*SOLVED* Checkbox ticks not counting using php count function


Recommended Posts

Hi Guys

I have a web page that uses php to echo out a checkbox for each entry in a cashbook. Idea is that a user can select multiple items and click a delete button. This button invokes a javascript function whcih asks users are you sure you want ot delete selected items (also displays item details - hence the 3 variables in the value of each checkbox).

Anyhow all works fine and the page submits and calls the correct code section to remove items (i.e. when task = remove_items set by the javascript function ViewAll)

But here is the error that is wrecking my head (as I have this approach working in other pages) it refuses to count the number of cheackboexes that a user has clicked - its is always at 0.

Looks like I am missing something or not getting something.

Any help as always will be gratly appreciated.

Fergal.

Snippets of Code:


$task = $_POST['task'];

if($task == "remove_items")
{
echo "Count is " . count($deleteitem); // HERE IS WHERE ISSUE IS
if (count($deleteitem) >= 1)
{
for ($i=0; $i < count($deleteitem); $i++)
{
$deldets = explode("*", $deleteitem[$i]);
$rID = $deldets[0];
echo "Record ID is " . $rID;
$result = mysql_query("DELETE FROM cash_book WHERE RecordID = '$rID'");
if (!$result) { die('Error in deletion: ' . mysql_error()); }
}
$message = 2;
}
}

<script type="text/javascript">
<!--
function viewAll()
{
var LB = "\n";
var msg= "";

for(i=0; i < cashbook["deleteitem[]"].length; i++)
{
if(cashbook["deleteitem[]"][i].checked == true)
{
var temp = cashbook["deleteitem[]"][i].value;
var temp2 = temp.split("*");
msg += temp2[0] + " " + temp2[1] + " for amount: " + temp2[2] + LB;
}
}

if(msg == "")
{
alert("You have not selected any items to delete.");
}
else
{
if (confirm("Do you really want to delete these Item(s)? (OK = Yes Cancel = No)" + LB + LB + msg))
{
document.cashbook.task.value = "remove_items";
document.cashbook.submit();
}
}
}

//-->
</script>

<form name="cashbook" method="POST" action="cash_book.php">

<?php
while($data = mysql_fetch_array($query1))
{

// display cash book data

<?php echo "<input type=\"checkbox\" name=\"deleteitem[]\" value=\"" . $data['RecordID'] . "*" . $data['Item'] . "*" . $data['Cash_In'] . "\" />\n"; ?>

}
?>

<input name="button" class = "formitems" type="button" value="Delete Selected" onclick="viewAll()">
You should be looking in the $_POST array here.

Your code:
[code]<php
f($task == "remove_items")
{
echo "Count is " . count($deleteitem); // HERE IS WHERE ISSUE IS
if (count($deleteitem) >= 1)
?>[/code]
change to
[code]<?php
f($task == "remove_items")
{
echo "Count is " . count($_POST['deleteitem']); // HERE IS WHERE ISSUE IS
if (count($_POST['deleteitem']) > 0)
?>[/code]

Ken
Ken

Brilliant once again.

But now I tried a few options on the explode to get the recordID:

Heres my latest offering:

if($task == "remove_items")
{
echo "Count is " . count($_POST['deleteitem']);

if (count($_POST['deleteitem']) > 0)
{
for ($i=0; $i < count($_POST['deleteitem']); $i++)
{
$deldets = explode("*", $_POST["$deleteitem[$i]"]);
$rID = $deldets[0];
echo "Record ID is " . $rID;
$result = mysql_query("DELETE FROM cash_book WHERE RecordID = '$rID'");
if (!$result) { die('Error in deletion: ' . mysql_error()); }
}
$message = 2;
}
}

Maybe I am using " in the post on the explode function?

Thanks,

Fergal.

[!--quoteo(post=353224:date=Mar 9 2006, 07:58 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 9 2006, 07:58 AM) [snapback]353224[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You should be looking in the $_POST array here.

Your code:
[code]<php
f($task == "remove_items")
{
echo "Count is " . count($deleteitem); // HERE IS WHERE ISSUE IS
if (count($deleteitem) >= 1)
?>[/code]
change to
[code]<?php
f($task == "remove_items")
{
echo "Count is " . count($_POST['deleteitem']); // HERE IS WHERE ISSUE IS
if (count($_POST['deleteitem']) > 0)
?>[/code]

Ken
[/quote]
Re-write this line
[code]<?php $deldets = explode("*", $_POST["$deleteitem[$i]"]);  ?>[/code]
as
[code]<?php $deldets = explode("*", $_POST['deleteitem'][$i]);  ?>[/code]
The array is "[b][!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$_POST['deleteitem'][!--colorc--][/span][!--/colorc--][/b]" so you need to index the entire name.

Ken
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.