Cheslia Posted June 28, 2017 Share Posted June 28, 2017 (edited) I have a timeclock application where you can add entries into a table. One of the columns is a <select> dropdown tag. If you do not enter one of the required fields, I redisplay the values entered so the user can correct the entries and resubmit to save them. Everything saves (posts) except the <select> dropdown value. The dropdown does show properly, even with the properly selected value. But if you change the value, it doesn't update. It retains the original value. Here is the function from STOP_Timeclock.php class that does the form: public function add_entries_page($msg) { $redisp = $_SESSION['redisp']; $out = ''; $offhrs = "N"; $dbh = new Database; //display the last entry the user saved to the database $dbh->query("SELECT * FROM Entries WHERE UID = :uid and GID = :gid order by Date DESC Limit 1"); $dbh->bind(':uid', $_SESSION['UID']); $dbh->bind(':gid', $_SESSION['GID']); $rows = $dbh->resultset(); foreach ($rows as $value) { $msg .= "Your last entry was: " . date('m-d-Y',strtotime($value['Date'])) . " for: " . $value['Description'] . " - " . number_format($value['Hours'],2,'.','') . " hour(s) were recorded. <br>"; } $select = $this->getGroupsForEntriesForm($_SESSION['UID']); //$select2 = $this->getGroupsForEntriesForm3($_SESSION['UID'],$_SESSION['array_category']); $caption = 'If you leave the Date Box empty, it defaults to today. *<b>Date/Category/Description/Hours are REQUIRED fields</b> <br><br>'; $head = array(array('name' => 'head', '1' => 'Date *', '2' => 'Ticket ID', '3' => 'Ticket Name', '4' => 'Category *', '5' => 'Description of Work Done *', '6' => 'Hours *', '7' => 'Off Hours(Y/N)' )); if($redisp == 0) { $body = array(); unset($_SESSION['array_id']); unset($_SESSION['array_name']); unset($_SESSION['array_category']); unset($_SESSION['array_desc']); unset($_SESSION['array_hours']); unset($_SESSION['array_offhrs']); for ($i = 1; $i < 11; $i++) { $row = array($this->xhtml->input(array('type' => 'text', 'id' => "datepicker$i", 'class' => 'entries_field_1', 'name' => 'DATE[]' )), $this->xhtml->input(array('type' => 'text', 'name' => 'ID[]', 'class' => 'entries_field_2' )), $this->xhtml->input(array('type' => 'text', 'name' => 'NAME[]', 'class' => 'entries_field_3' )), $select, $this->xhtml->input(array('type' => 'text', 'name' => 'DESC[]', 'class' => 'entries_field_5' )), $this->xhtml->input(array('type' => 'text', 'name' => 'HOURS[]', 'class' => 'entries_field_6' )), $this->xhtml->input(array('type' => 'text', 'name' => 'OFFHOURS[]', 'value' => $offhrs, 'class' => 'entries_field_7' )) ); $body[$i] = $row; } } if($redisp == 1) { $body = array(); $name = $_SESSION['array_name']; $id = $_SESSION['array_id']; $category = $_SESSION['array_category']; $desc = $_SESSION['array_desc']; $hours = $_SESSION['array_hours']; $offhrs = $_SESSION['array_offhrs']; $dates = $_SESSION['array_dates']; $max_count = $_SESSION['max_count']; for ($i = 0; $i < $max_count; $i++) { $selected = $this->getCatName($_SESSION['UID'], $category[$i]); $row = array($this->xhtml->input(array('type' => 'text', 'id' => "datepicker$i", 'class' => 'entries_field_1', 'value' => $dates[$i], 'name' => 'DATE[]' )), $this->xhtml->input(array('type' => 'text', 'name' => 'ID[]', 'value' => "$id[$i]", 'class' => 'entries_field_2' )), $this->xhtml->input(array('type' => 'text', 'name' => 'NAME[]', 'value' => "$name[$i]", 'class' => 'entries_field_3' )), "$selected", $this->xhtml->input(array('type' => 'text', 'name' => 'DESC[]', 'value' => "$desc[$i]", 'class' => 'entries_field_5' )), $this->xhtml->input(array('type' => 'text', 'name' => 'HOURS[]', 'value' => "$hours[$i]", 'class' => 'entries_field_6' )), $this->xhtml->input(array('type' => 'text', 'name' => 'OFFHOURS[]', 'value' => "$offhrs[$i]", 'class' => 'entries_field_7' )), ); $body[$i] = $row; if(isset($_POST['CAT'])) { $_SESSION['cats'][$i] = $_POST['CAT']; } else { $_SESSION['cats'][$i] = $category[$i]; } } } $data = array_replace($head, $body); $table = $this->xhtml->table2($data, $caption, array('class' => 'add_time')); $table .= $this->xhtml->input(array('type' => 'submit', 'value' => 'Add Entries' )); $form = $this->xhtml->form(array(array($table), ), array('action' => BASE . '/addEntries.php', 'class' => 'add_time', 'onsubmit' => 'return valid_Add(this)', 'method' => 'post' ) ); $out = $form; $menu = $this->getMenu(); $msg = $this->xhtml->div("$msg", array('id' => 'error')); $out = $this->xhtml->div("$out", array('id' => 'add_entries_form')); $out = $this->xhtml->div("$msg$out", array('id' => 'main')); $out = $this->xhtml->div("$menu$out", array('id' => 'container')); $out = $this->xhtml->body("$out", array('onload' => 'startUp();')); return $out; } And here is the addEntries page that evaluates for all fields and then saves to the database if correct. <?php session_start(); include('includes/config.php'); $msg =''; $x=0; $ok_to_post = 0; $page = new STOP_Timeclock; $xhtml = new XHTML; $dbh = new Database; if (isset($_POST['NAME'])) { $name = array_filter($_POST['NAME']); $_SESSION['array_name'] = $name; } if (isset($_POST['ID'])) { $id = array_filter($_POST['ID']); $_SESSION['array_id'] = $id; } if($_SESSION['redisp'] == 0) { if (isset($_POST['CAT'])) { $category = array_filter($_POST['CAT']); $_SESSION['array_category'] = $category; } } else { $category = $_SESSION['cats']; } if (isset($_POST['DESC'])) { $desc = array_filter($_POST['DESC']); $_SESSION['array_desc'] = $desc; } if (isset($_POST['HOURS'])) { $hours = array_filter($_POST['HOURS']); $_SESSION['array_hours'] = $hours; } if (isset($_POST['OFFHOURS'])) { $offhrs = array_filter($_POST['OFFHOURS']); $_SESSION['array_offhrs'] = $offhrs; } if (isset($_POST['DATE'])) { $dates = array_filter($_POST['DATE']); $_SESSION['array_dates'] = $dates; } $count_category = count($category); //$msg .= "count of category field is " . $count_category . "<br>"; $count_desc = count($desc); //$msg .= "count of desc field is " . $count_desc . "<br>"; $count_hours = count($hours); //$msg .= "count of hours field is " . $count_hours . "<br>"; $count_dates = count($dates); //$msg .= "count of date field is " . $count_dates . "<br>"; if ($count_desc == $count_hours) { if ($count_hours == $count_dates) { $ok_to_post = 1; $max_count = $count_desc; } else { if ($count_hours > $count_dates) { $max_count = $count_desc; } else { $max_count = $count_hours; } $ok_to_post = 0; $msg .= 'You did not enter one of the required fields - Date/Category/Description/Hours are REQUIRED. <br />'; } } else { if ($count_desc > $count_hours) { $max_count = $count_desc; } else { $max_count = $count_hours; } $ok_to_post = 0; $msg .= 'You did not enter one of the required fields - Date/Category/Description/Hours are REQUIRED. <br />'; } //$msg .= "max count is set to " . $max_count . "<br>"; $_SESSION['max_count'] = $max_count; //everything is entered correctly - now process and insert into the database $rowCount = 0; $today = date("Y-m-d"); $count = 0; if ($ok_to_post == 1) { $redisp = 0; $_SESSION['redisp'] = $redisp; $dbh->beginTransaction(); $myquery = "INSERT INTO Entries (`UID`, `GID`, `Date`, `CID`, `TicketNumber`, `TicketName`, `Description`, `Hours`, `OffHours`,`Created_Date`) VALUES (:uid, :gid, :date, :cid, :num, :name, :desc, :hrs, :offhrs, now())"; $dbh->query($myquery); for ($x = 0; $x < $max_count; $x++) { $dbh->bind(':uid', $_SESSION['UID']); $dbh->bind(':gid', $_SESSION['GID']); $dbh->bind(':date', $dates[$x]); $msg .= "category[x] is set to " . $category[$x] . "<br>"; $dbh->bind(':cid', $category[$x]); if (count($id)==0) { $dbh->bind(':num', NULL); } else { $dbh->bind(':num', $id[$x]); } if (count($name)==0) { $dbh->bind(':name', NULL); } else { $dbh->bind(':name', $name[$x]); } $dbh->bind(':desc', $desc[$x]); $msg .= "desc[x] is set to " . $desc[$x] . "<br>"; $dbh->bind(':hrs', $hours[$x]); $msg .= "hour[x] is set to " . $hours[$x] . "<br>"; $dbh->bind(':offhrs', $offhrs[$x]); $result = $dbh->execute(); if (!$result) { $msg .= "Saving entry to the database failed. Please see system administrator. <br />"; continue; } $count += $dbh->rowCount(); } if ($count != $max_count) { $dbh->cancelTransaction(); $msg .= 'An error occurred and all entries could not be added. <br /> For safety, this was rolled back and no entries were added. <br /> Please see system administrator. <br />'; } else { $dbh->endTransaction(); $dbh->query("SELECT * FROM Entries WHERE UID = :uid and GID = :gid and Created_Date >= :date2"); $dbh->bind(':uid', $_SESSION['UID']); $dbh->bind(':gid', $_SESSION['GID']); $dbh->bind(':date2', $today); $rows = $dbh->resultset(); $msg .= "Returned " . count($rows) . " entries successfully written to and retrieved from the database for you today. <br>"; $tothrs = 0; foreach ($rows as $value) { $msg .= "On " . date('m-d-Y',strtotime($value['Date'])) . " for " . $value['Description'] . " - " . number_format($value['Hours'],2,'.','') . " hour(s) were recorded. <br>"; $tothrs = $tothrs + $value['Hours']; } $msg .= "Total hours entered today = " . $tothrs . "<br>"; $msg .= "<br><br>"; } $dbh = NULL; $_SESSION['MSG'] = $msg; header('Location: ' . BASE); } else { $redisp = 1; $_SESSION['redisp'] = $redisp; $_SESSION['MSG'] = $msg; header('Location: ' . BASE); } ?> I am hoping that you all might be able to assist. Thanks in advance. Edited June 28, 2017 by Cheslia Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2017 Share Posted June 28, 2017 That is a LOT of code to expect us to sift through and try to make sense of. You don't even tell us what the name of the select field is. The first code block doesn't actually create any form fields from what I can tell. The second code block doesn't have any Update queries, so your problem of "But if you change the value, it doesn't update. It retains the original value." doesn't make sense. By making some assumptions I think your problem may be that if the user submits any missing/incorrect values you re-display the form with the previous values and once the user corrects the form and resubmit the field (whichever one it is) is being saved with the originally selected value (before the user made corrections). Quote Link to comment Share on other sites More sharing options...
Cheslia Posted June 28, 2017 Author Share Posted June 28, 2017 Sorry about that....I maintain code that someone else wrote and it is a different style than I am used to. The field in question is the $category or $_POST['CAT'] field. I guess I left out one important function that actually builds that dropdown box with values. Here is that function that is called by getCatName(). I hope that helps.... public function getCatName($uid, $cat) { $out = ''; $cats = array(); $dbh = $this->rdb; $dbh->query("SELECT Categories FROM Users WHERE ID = :id"); $dbh->bind(':id', $uid); $row = $dbh->single(); $user_cats = (isset($row['Categories']) ? $row['Categories'] : ''); if ($user_cats == '') { $dbh->query("SELECT GID FROM Users WHERE ID = :id"); $dbh->bind(':id', $uid); $row = $dbh->single(); $gid = $row['GID']; $cats = $this->getCategories2($gid, $cat); } else { $dbh->query("SELECT ID, Name FROM Categories WHERE ID IN ($user_cats) ORDER BY Name"); $rows = $dbh->resultset(); foreach ($rows as $row) { if ($row['ID'] == "$cat") { $cats[$row['Name']] = array('value' => $row['ID'], 'selected' => 'selected' ); } else { $cats[$row['Name']] = array('value' => $row['ID']); } } } $dbh = NULL; return $this->xhtml->select($cats, array('name' => 'CAT', 'class' => 'entries_field_4')); } The way it works is this....when you initially load the page, it does the $redisp==0 portion of the add_entries_page(). It creates the table with everything blank except the categories drop down box (which is built by a very similar function as the one just above with a couple of minor variations). And when you hit the Add Entries button, it checks in the addEntries.php to make sure all the required fields were filled in. And if not, then it goes back to the add_entries_page() and does the portion of the code in the if $redisp == 1. Again, building the table with the remembered values and the already selected choices from the dropdown $categories. But is they change the category selection, it doesn't update that value. All other values are updated, just not that selection from the dropdown. Hope that helps a bit more. Sorry....I didn't want to not post something critical in the code/logic that would end up being needed. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 28, 2017 Share Posted June 28, 2017 Sorry, but I'm just not going to take the time to try an d decipher that code. But, here is some feedback: When the user submits the data with missing fields is the page refreshed and the form re-created or is the error handling done via JavaScript. If the latter, then the problem is likely in the JavaScript. If the page/form is regenerated I would inspect the HTML code to verify that the Select list options have the values that you expect. Then, using Chrome's debugging, resubmit the form with a different value for that select option and verify what is being sent in the POST data. If it is the value you expect, then you know the problem is in the code that processes the form data. If it is not the expected value then it could be in the code that creates the form or with some JS on the page. Quote Link to comment Share on other sites More sharing options...
Cheslia Posted June 28, 2017 Author Share Posted June 28, 2017 Yeah I know it was a lot. I tried to make a "mockup" of just the details, but can't seem to skinny it down enough to reproduce the issue. But thanks for the advice. I'll keep digging or trying to find another workaround. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 28, 2017 Share Posted June 28, 2017 For some strange reason, the target script will ignore the CAT parameter if the form was redisplayed and instead use the old value from the session: if($_SESSION['redisp'] == 0) { ... } else { $category = $_SESSION['cats']; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } That doesn't look right. 1 Quote Link to comment Share on other sites More sharing options...
Cheslia Posted July 6, 2017 Author Share Posted July 6, 2017 That was my attempt at a workaround. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 6, 2017 Share Posted July 6, 2017 When you want help with code, don't turn that code into a moving target. Post the original, unaltered version, not your workarounds which may have introduced new problems. Your current code doesn't add up at all. There's not just one problem but a whole collection of defects which need to be fixed one by one. Did you manage to do that? Or are you still having issues? 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.