Jump to content

Archived

This topic is now archived and is closed to further replies.

fohanlon

*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()">

Share this post


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

Share this post


Link to post
Share on other sites
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]

Share this post


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

Share this post


Link to post
Share on other sites

×

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.