spertuit Posted July 1, 2013 Share Posted July 1, 2013 Okay, I cant seem to wrap my head around this. I have checkboxes created from javascript var cell2 = row.insertCell(1); var element2 = document.createElement("input"); element2.type = "checkbox"; element2.name="breakfast[]"; cell2.appendChild(element2); The values are stored in an array and read by a function. The problem I have is on the form side. If the checkboxes say yes, no, yes ... The value no gets skipped and the array contains yes,yes Here is the submit function on the form if(isset($_REQUEST['insert'])){ extract($_REQUEST); if($obj->createMeal($id,'meals')); { //then condition } } How can I handle a checkbox that is not checked and have "no" placed as a post variable in my array? Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted July 1, 2013 Share Posted July 1, 2013 if the check box is not checked it wont send that id in the array Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 1, 2013 Share Posted July 1, 2013 How can I handle a checkbox that is not checked and have "no" placed as a post variable in my array? For what purpose? You already have a list of the checked options, so you know all the others were unchecked. Depending on how you are using the data, there are different solutions for what you need. Quote Link to comment Share on other sites More sharing options...
spertuit Posted July 2, 2013 Author Share Posted July 2, 2013 I have several array created breakfast, lunch, dinner, and midnight. The user inputs the guest name and checks what meals they ate. When these values are passed in from the form I only receive the checked items I expect: breakfast on,off, on, on lunch on,off,off,on dinner on, on, off, on midnight off, off ,off, on but I get breakfast on,on,on lunch on,on dinner on,on,on midnight on so when i loop through by guest name through my arrays the one an off values due not correspond with the correct rows. I would need an associative array passed in instead of a basic array. Quote Link to comment Share on other sites More sharing options...
spertuit Posted July 2, 2013 Author Share Posted July 2, 2013 <!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=utf-8" /> <title>Create A Meal Ticket</title> <link rel="stylesheet" type="text/css" href="Stylesheets/reset.css"> <link rel="stylesheet" type="text/css" href="Stylesheets/style.css"> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <SCRIPT language="javascript"> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var cell1 = row.insertCell(0); cell1.style.textAlign = 'left'; cell1.innerHTML = "<a onClick=\"deleteRow('dataTable', this.parentNode.parentNode.rowIndex)\" align=\"left\"> Remove this Guest</a>"; var cell2 = row.insertCell(1); var element2 = document.createElement("input"); element2.type = "checkbox"; element2.name="breakfast[]"; cell2.appendChild(element2); var cell3 = row.insertCell(2); var element2 = document.createElement("input"); element2.type = "checkbox"; element2.name="lunch[]"; cell3.appendChild(element2); var cell4 = row.insertCell(3); var element2 = document.createElement("input"); element2.type = "checkbox"; element2.name="dinner[]"; cell4.appendChild(element2); var cell5 = row.insertCell(4); var element2 = document.createElement("input"); element2.type = "checkbox"; element2.name="midnight[]"; cell5.appendChild(element2); var cell6 = row.insertCell(5); var element2 = document.createElement("input"); element2.type = "checkbox"; element2.name = "lodging[]"; cell6.appendChild(element2); var cell7 = row.insertCell(6); var element2 = document.createElement("input"); element2.type = "text"; element2.name = "guestName[]"; cell7.appendChild(element2); } function deleteRow(tableID, row) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; table.deleteRow(row); }catch(e) { alert(e); } } </SCRIPT> </head> <body> <?php function __autoload($class){ include_once("includes/functions.php"); } error_reporting(E_ALL); ini_set('display_errors', '1'); $obj=new oopCrud; if(isset($_REQUEST['insert'])){ extract($_REQUEST); if($obj->createMeal($id,'meals')); { //then condition } } if(isset($_REQUEST['update'])){ if($obj->updateMeal($_REQUEST['update'],"meals")){ echo"Meals have been updated"; } } if(isset($_REQUEST['del_id'])){ if($obj->deleteMeal($_REQUEST['del_id'],"meals")){ echo"Selected guest has been removed"; } } foreach($obj->logDetails($_REQUEST['mealTicket'],"dailymasterlogs","vessels") as $value){ extract($value); ?> <form action="mealTickets.php?mealTicket=<?php echo $id ?>&&id=<?php echo $id ?>" method="post"> <table border=1 class="hovertable"> <tr class="top row"> <td colspan="5"> <a href="index.php"><button class="btn">Home<img src="images/adriatic-logo.png" width="25" /></button></a> </td> </tr> <tr> <th colspan="4" style="text-align:center;"><h1>Company Name</h1> </tr> <tr class="outside"> <td colspan="7" text-align="center"><h2>Current Master Log</h2></td> </tr> <tr class="outside"> <td width="18%" style="text-align:right">M/V:</td> <td width="26%"><?php echo $logVesselID; ?></td> <td width="18%" style="text-align:right">Date:</td> <td width="38%"><?php echo $logMonth . " " . $logDay . " " . $logYear; ?></td> </tr> <tr class="bottom"> <td colspan="4" text-align="center"><h2><?php echo $logEmployer; ?></h2></td> </tr> </table> <form id = "meals" name="meals" action="mealTickets.php?mealTicket=<?php echo $id; ?>&&update=<?php echo $id ?>" method="post"> <table id="dataTable" class="hovertable"> <tr class="outside"> <td colspan="7" text-align="center"><h2>Guest List</h2></td> </tr> <tr class="outside"> <td><input type="button" value="Add Row" class="orangeButton" onclick="addRow('dataTable')" /></td> <td>Breakfast</td> <td>Lunch</td> <td>Dinner</td> <td>Midnight</td> <td>Lodging</td> <td>Guest Name</td> </tr> <?php foreach($obj->showMeals($id,"meals") as $value){ extract($value); ?> <tr class="success"> <td><a href="mealTickets.php?mealTicket=<?php echo $masterLogID ?>&&del_id=<?php echo $id ?>"><button class="btn" onclick="return confirm('Are you sure you wish to delete this Guest?');">Remove this Guest</a></td> <td> <?php if($breakfast == "on"){?> <input type="checkbox" name="breakfast[]" checked="checked" class="checkbox"/> <?php }else{ ?> <input type="checkbox" name="breakfast[]" class="checkbox"/> <?php } ?> </td> <td> <?php if($lunch == "on"){?> <input type="checkbox" name="lunch[]" checked="checked " class="checkbox" /> <?php }else{ ?> <input type="checkbox" name="lunch[]" class="checkbox" /> <?php } ?> </td> <td> <?php if($dinner == "on"){?> <input type="checkbox" name="dinner[]" checked="checked" /> <?php }else{ ?> <input type="checkbox" name="dinner[]" /> <?php } ?> </td> <td> <?php if($midnight == "on"){?> <input type="checkbox" name="midnight[]" checked="checked" /> <?php }else{ ?> <input type="checkbox" name="midnight[]" /> <?php } ?> </td> <td> <?php if($lodging == "on"){?> <input type="checkbox" name="lodging[]" checked="checked" /> <?php }else{ ?> <input type="checkbox" name="lodging[]" /> <?php } ?> </td> <td><input type="text" name="guestName[]" value = "<?php echo $guestName; ?>"</td> </tr> <?php } ?> </table> <table class="hovertable" border="1"> <tr class="outside"> <td colspan="7" text-align="center"><h2>Totals</h2></td> </tr> <tr class="outside"> <td style="text-align:right"> Company Representative Name:</td> <td><input type="text" name="repName" value="<?php echo $repName; ?>"/></td> <td style="text-align:right">Company Representative Title:</td> <td><input type="text" name="repTitle" value="<?php echo $repTitle; ?>"/></td> </tr> <tr class="outside"> <?php $meal1 = $obj->fieldTotal($masterLogID,"breakfast","meals") ; $meal2 = $obj->fieldTotal($masterLogID,"lunch","meals") ; $meal3 = $obj->fieldTotal($masterLogID,"dinner","meals") ; $meal4 = $obj->fieldTotal($masterLogID,"midnight","meals") ; $totalMeals = $meal1 + $meal2 + $meal3 + $meal4; $totalLodging = $obj->fieldTotal($masterLogID,"lodging","meals") ; ?> <td style="text-align:right">Total Meals:</td> <td><?php echo $totalMeals; ?></td> <td style="text-align:right">Total Lodging:</td> <td><?php echo $totalLodging; ?></td> </tr> <tr class="outside"> <td style="text-align:right">Captain:</td> <td><?php echo $logCaptain; ?></td> <td style="text-align:right">Vessel Cook:</td> <td><?php echo $logCook; ?></td> </tr> <tr class="outside"> <td colspan="4"></td> </tr> <tr class="bottom"> <td colspan="2" text-align="center"><a href="edit.php?id=<?php echo $id; ?>"><button class="btn">Cancel<img src="images/EditLog.png" width="35" /></button></a> <td colspan="2" text-align="center"><img src="images/save.png" width="25" /><input type="submit" name="insert" value="Save" class="btn"><img src="images/save.png" width="25" /></td> </tr> </table> </form> <?php } ?> </body> </html> Php function to receive checkbox arrays public function createMeal($id,$table){ $breakfast = array(); $lunch = array(); $dinner = array(); $midnight = array(); $lodging = array(); $guestName = $_POST['guestName']; if(isset($_POST['breakfast'])){ $breakfast = $_POST['breakfast']; } if(isset($_POST['lunch'])){ $lunch = $_POST['lunch']; } if(isset($_POST['dinner'])){ $dinner = $_POST['dinner']; } if(isset($_POST['midnight'])){ $midnight = $_POST['midnight']; } if(isset($_POST['lodging'])){ $lodging = $_POST['lodging']; } $repName = $_POST['repName']; $repTitle = $_POST['repTitle']; $masterLogID = $id; $this->deleteMealLog($id,'meals'); foreach($guestName as $a => $b) { if(isset($breakfast[$a])){ $breakfastLog = $breakfast[$a]; }else{ $breakfastLog = "off"; } if(isset($lunch[$a])){ $lunchLog = $lunch[$a]; }else{ $lunchLog = "off"; } if(isset($dinner[$a])){ $dinnerLog = $dinner[$a]; }else{ $dinnerLog = "off"; } if(isset($midnight[$a])){ $midnightLog = $midnight[$a]; }else{ $midnightLog = "off"; } if(isset($lodging[$a])){ $lodgingLog = $lodging[$a]; }else{ $lodgingLog = "off"; } $sql="INSERT INTO meals SET repName=:repName,repTitle=:repTitle, guestName=:guestName, breakfast=:breakfast, lunch=:lunch, dinner=:dinner, midnight=:midnight, lodging=:lodging, masterLogID=:masterLogID"; if(!$stmt = $this->conn->prepare($sql)){ // prepare failed echo "<pre>Prepare failed:\n"; print_r($pdo->errorInfo()); echo "</pre>"; } else { if(!$stmt->execute(array(':repName'=>$repName,':repTitle'=>$repTitle,':guestName'=>$guestName[$a],':breakfast'=>$breakfastLog,':lunch'=>$lunchLog,':dinner'=>$dinnerLog,':midnight'=>$midnightLog,':lodging'=>$lodgingLog,':masterLogID'=>$masterLogID))){ // execute failed echo "<pre>Execute failed:\n"; print_r($stmt->errorInfo()); echo "</pre>"; } } echo "<pre>Meal Log Updated:\n"; echo "</pre>"; print_r($breakfast); } } public function updateMeal($id,$table){ $breakfast = array(); $lunch = array(); $dinner = array(); $midnight = array(); $lodging = array(); $guestName = $_POST['guestName']; if(isset($_POST['breakfast'])){ $breakfast = $_POST['breakfast']; } if(isset($_POST['lunch'])){ $lunch = $_POST['lunch']; } if(isset($_POST['dinner'])){ $dinner = $_POST['dinner']; } if(isset($_POST['midnight'])){ $midnight = $_POST['midnight']; } if(isset($_POST['lodging'])){ $lodging = $_POST['lodging']; } $masterLogID = $id; foreach($guestName as $a => $b) { if(isset($breakfast[$a])){ $breakfastLog = $breakfast[$a]; }else{ $breakfastLog = "off"; } if(isset($lunch[$a])){ $lunchLog = $lunch[$a]; }else{ $lunchLog = "off"; } if(isset($dinner[$a])){ $dinnerLog = $dinner[$a]; }else{ $dinnerLog = "off"; } if(isset($midnight[$a])){ $midnightLog = $midnight[$a]; }else{ $midnightLog = "off"; } if(isset($lodging[$a])){ $lodgingLog = $lodging[$a]; }else{ $lodgingLog = "off"; } $sql="UPDATE meals SET guestName=:guestName, breakfast=:breakfast, lunch=:lunch, dinner=:dinner, midnight=:midnight, lodging=:lodging WHERE masterLogID=:masterLogID"; if(!$stmt = $this->conn->prepare($sql)){ // prepare failed echo "<pre>Prepare failed:\n"; print_r($pdo->errorInfo()); echo "</pre>"; } else { if(!$stmt->execute(array(':guestName'=>$guestName[$a],':breakfast'=>$breakfastLog,':lunch'=>$lunchLog,':dinner'=>$dinnerLog,':midnight'=>$midnightLog,':lodging'=>$lodgingLog,':masterLogID'=>$masterLogID))){ // execute failed echo "<pre>Execute failed:\n"; print_r($stmt->errorInfo()); echo "</pre>"; } } } } Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 2, 2013 Share Posted July 2, 2013 I expect: breakfast on,off, on, on lunch on,off,off,on dinner on, on, off, on midnight off, off ,off, on but I get breakfast on,on,on lunch on,on dinner on,on,on midnight on so when i loop through by guest name through my arrays the one an off values due not correspond with the correct rows. I would need an associative array passed in instead of a basic array. As already stated only CHECKED checkboxes are passed in the form data. This has been the behavior for decades. You do not need an array of a bunch of yes/no values. You are trying to fix a problem that doesn't exist. There are better solutions than what you are trying to do. You should never try to associate data based upon its position in a list. The problem is in how you are creating the form fields and how you are processing the data. Second, you shouldn't be storing string values of On and Off and should instead be storing the logical value of o and 1 that can be interpreted as Boolean values. I can't really make heads or tails of your form since it is dynamically created. But, a simple solution to what you should be doing is something like this: $lunch = (isset($_POST['lunch'])) ? 1 : 0; If 'lunch' is set in the POST data (in other words it was checked) it will set the value to 1, else the value will be 0. Quote Link to comment Share on other sites More sharing options...
spertuit Posted July 2, 2013 Author Share Posted July 2, 2013 I apologize because my wording to explain this is vague. I wanted to store 10 and 0's, but Ive been writing and rewriting code to try and get this right. Imagine this guest1 breakfast lunch dinner midnight guest2 breakfast lunch dinner midnight guest3 breakfast lunch dinner midnight now if i check that guest 1 and guest 3 have breakfast and guest 2 didn't have breakfast the values are stored in the breakfast array as breakfast[1,1] and just skips guest 2 Quote Link to comment Share on other sites More sharing options...
spertuit Posted July 2, 2013 Author Share Posted July 2, 2013 If they were textboxes I would use this function: public function createMeal($id,$table){ try{ //write query $breakfast = $_POST['breakfast']; $lunch = $_POST['lunch']; $dinner = $_POST['dinner']; $midnight = $_POST['midnight']; $lodging = $_POST['lodging']; $guestName = $_POST['guestName']; $repName = $_POST['repName']; $repTitle = $_POST['repTitle']; $masterLogID = $id; $this->deleteMealLog($id,'meals'); foreach($guestName as $a => $b) { $query = "INSERT INTO trips SET breakfast = ?, lunch = ?, dinner = ?, midnight = ?,lodging = ?,guestName = ?,repName = ?,repTitle = ?,masterLogID = ?"; $stmt = $this->conn->prepare($query); $stmt->bindParam(1,$breakfast[$a]); $stmt->bindParam(2,$lunch[$a]); $stmt->bindParam(3,$dinner[$a]); $stmt->bindParam(4,$midnight[$a]); $stmt->bindParam(5,$lodging[$a]); $stmt->bindParam(6,$guestName[$a]); $stmt->bindParam(7,$repName[$a]); $stmt->bindParam(8,$repTitle[$a]); $stmt->bindParam(9,$id); // Execute the query $stmt->execute(); } echo "Record was saved."; }catch(PDOException $exception){ //to handle error echo "Error: " . $exception->getMessage(); } } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 2, 2013 Share Posted July 2, 2013 you are going to a lot of extra trouble. you should only store data that exists (see post #2 in your last thread.) if you still want to store a 0 value for checkboxes that are not checked, it is a simple if/else statement to produce the the correct 1 or 0 value based on a checkbox being set. your form fields, because you are not specifying an index value, are also are not in sync with the guest name anytime you don't check one of the checkboxes, because the boxes that are checked will be sequentially numbered (see post #9 in your last thread.) Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 3, 2013 Share Posted July 3, 2013 Yep, as mac_gyver states, you are simply doing it wrong. I previously stated You should never try to associate data based upon its position in a list. . . . which is exactly what you are doing. You have multiple guests and you want to indicate which ones have had which meals. This is a very simple problem - you just need to name the fields appropriately. In this case you are entering records for each GUEST, so the values should be constructed to make that processing easy. But, you still have the problem that if no boxes are checked for a guest then you don't have any data in the POST data to insert a record with 0 values. The solution is easy - create a hidden field on the form for each guest ID. Here is an example of how the input fields could be constructed for a single guest: Guest ID5: <input type="hidden" name="guests[]" value="5" /> <input type="checkbox" name="breakfast[5]" value="1" /> <input type="checkbox" name="lunch[5]" value="1" /> <input type="checkbox" name="dinner[5]" value="1" /> <input type="checkbox" name="midnight[5]" value="1" /> Then to process ALL the guests (even those that did not have any meals) you could do something like this: foreach($_POST['guests'] as $guestID) { $breakfast = (isset($_POST['breakfast'][$guestID])) ? 1 : 0; $lunch = (isset($_POST['lunch'][$guestID])) ? 1 : 0; $dinner = (isset($_POST['dinner'][$guestID])) ? 1 : 0; $midnight = (isset($_POST['midnight'][$guestID])) ? 1 : 0; //Run query to insert record } Quote Link to comment 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.