fohanlon Posted March 9, 2006 Share Posted March 9, 2006 Hi GuysI 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()"> Quote Link to comment https://forums.phpfreaks.com/topic/4520-solved-checkbox-ticks-not-counting-using-php-count-function/ Share on other sites More sharing options...
kenrbnsn Posted March 9, 2006 Share Posted March 9, 2006 You should be looking in the $_POST array here.Your code:[code]<phpf($task == "remove_items"){echo "Count is " . count($deleteitem); // HERE IS WHERE ISSUE ISif (count($deleteitem) >= 1)?>[/code]change to[code]<?phpf($task == "remove_items"){echo "Count is " . count($_POST['deleteitem']); // HERE IS WHERE ISSUE ISif (count($_POST['deleteitem']) > 0)?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/4520-solved-checkbox-ticks-not-counting-using-php-count-function/#findComment-15749 Share on other sites More sharing options...
fohanlon Posted March 9, 2006 Author Share Posted March 9, 2006 KenBrilliant 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]<phpf($task == "remove_items"){echo "Count is " . count($deleteitem); // HERE IS WHERE ISSUE ISif (count($deleteitem) >= 1)?>[/code]change to[code]<?phpf($task == "remove_items"){echo "Count is " . count($_POST['deleteitem']); // HERE IS WHERE ISSUE ISif (count($_POST['deleteitem']) > 0)?>[/code]Ken[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/4520-solved-checkbox-ticks-not-counting-using-php-count-function/#findComment-15758 Share on other sites More sharing options...
kenrbnsn Posted March 9, 2006 Share Posted March 9, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4520-solved-checkbox-ticks-not-counting-using-php-count-function/#findComment-15786 Share on other sites More sharing options...
fohanlon Posted March 9, 2006 Author Share Posted March 9, 2006 I get it now. Excellent. Thanks, Fergal. Quote Link to comment https://forums.phpfreaks.com/topic/4520-solved-checkbox-ticks-not-counting-using-php-count-function/#findComment-15787 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.