travisco87 Posted April 21, 2014 Share Posted April 21, 2014 Hi All, I started learning PHP a couple years ago and decided to build a work order system for fun. I have been working on this but for some reason the 'post' does not seem to translate over into the next page. Here is my code below wo_view.php <?php require_once('authorize.php'); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html lang="en"> <head> <title>Admin Page</title> <link rel="stylesheet" type="text/css" href="css/adminmenu.css" /> <!-- Add jQuery library --> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> </head> <body> <?php include_once 'nav-menu.php' ?> <h2 style="text-align: center;">Work Order Information</h2> <div id="woTable"> <fieldset> <?php include 'includes.php'; if(isset($_POST['submit'])) { if(!empty($_POST)) { $wo_info = array( "first_name"=>Trim(stripcslashes($_POST['first_name'])), "last_name"=>Trim(stripcslashes($_POST['last_name'])), "phone_number"=>Trim(stripcslashes($_POST['phone_number'])), "email_addr"=>Trim(stripcslashes($_POST['email_addr'])), "strt_addr"=>Trim(stripcslashes($_POST['strt_addr'])), "strt_addr2"=>Trim(stripcslashes($_POST['strt_addr2'])), "city"=>Trim(stripcslashes($_POST['city'])), "state"=>Trim(stripcslashes($_POST['state'])), "zip"=>Trim(stripcslashes($_POST['zip'])), "service_type"=>Trim(stripcslashes($_POST['service_type'])), "service_date"=>$_POST['inputDate'], "service_time"=>Trim(stripcslashes($_POST['service_time'])), "service_notes"=>Trim(stripcslashes($_POST['service_notes'])), "wo_id"=>Trim(stripcslashes($_POST['wo_id'])), "wo_status"=>Trim(stripcslashes($_POST['wo_status'])) ); $sql = 'UPDATE workorderstbl SET first_name = :first_name, last_name = :last_name, phone_number = :phone_number, email_addr = :email_addr, strt_addr = :strt_addr, strt_addr2 = :strt_addr2, city = :city, state = :state, zip = :zip, service_type = :service_type, service_notes = :service_notes, wo_status = :wo_status WHERE wo_id = :wo_id LIMIT 1'; $params = array( ':first_name'=>$wo_info['first_name'], ':last_name'=>$wo_info['last_name'], ':phone_number'=>$wo_info['phone_number'], ':email_addr'=>$wo_info['email_addr'], ':strt_addr'=>$wo_info['strt_addr'], ':strt_addr2'=>$wo_info['strt_addr2'], ':city'=>$wo_info['city'], ':state'=>$wo_info['state'], ':zip'=>$wo_info['zip'], ':service_type'=>$wo_info['service_type'], ':service_notes'=>$wo_info['service_notes'], ':service_date'=>$wo_info['service_date'], ':service_time'=>$wo_info['service_time'], ':wo_id'=>$wo_info['wo_id'], ':wo_status' =>$wo_info['wo_status'] ); $stmt = $DBH->prepare($sql); $stmt->execute($params); redirect('wo_view.php?id=' . $_POST['wo_id']); } else { echo "\nPDO::errorInfo();\n"; print_r($DBH->errorInfo()); } } else { $postviewsql = "SELECT * FROM workorderstbl WHERE wo_id = :wo_id LIMIT 1"; $poststmt = $DBH->prepare($postviewsql); $poststmt->bindParam(':wo_id', $_GET['id'], PDO::PARAM_STR, 5); $poststmt->execute(); if(!($poststmt->rowCount())) { echo 'Work Order #' . $_GET['id'] . ' not found BOOM'; $DBH = null; } else { $row = $poststmt->fetch(PDO::FETCH_ASSOC); echo "<legend>Contact Info</legend>"; echo "<a href='adminworkorders.php' style='float: left;'>Go Back to All Service Request</a><a href='wo_edit.php?id=" . $row['wo_id'] . "&sd=" . $row['service_date'] . "' style='float: right;'>Edit Service Request</a><br /><br />"; echo "Service Request: <strong>" . $row['wo_id'] . "</strong>     Created On: <strong>" . date('F j<\s\up>S</\s\up>, Y', $row['date_posted']). "</strong>     Service Request Status: <strong>" . $row['wo_status'] . "</strong><br />"; echo "Name: <strong>" . $row['first_name'] . " " . $row['last_name'] . "</strong><br />"; echo "Phone Number: <strong>" . $row['phone_number'] . "</strong>     Email Address: <strong>" . $row['email_addr'] . "</strong>"; echo "</fieldset><fieldset><legend>Address</legend>"; echo "<strong>" . $row['strt_addr'] . " " . $row['strt_addr2'] . "</br>"; echo $row['city'] . " " . $row['state'] . ", " . $row['zip'] . "</strong><br />"; echo "</fieldset><fieldset><legend>Service Information:</legend>"; echo "Service Type: <strong>" . $row['service_type'] . "</strong>     Request Date: <strong>" . date('m-d-Y',$row['request_date']) . "</strong>     Requested Time Frame: <strong>" . $row['request_time'] . "</strong><br />"; echo "Service Date: <strong>" . $row['service_date'] . "</strong><br />"; echo "Service Notes: <br /><strong>" . nl2br($row['service_notes']) . "</strong><br /><br />"; $DBH = null; } } ?> </fieldset> </div> </body> </html> wo_edit.php <?php require_once('authorize.php'); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html lang="en"> <head> <title>Admin Page</title> <link rel="stylesheet" media="screen" type="text/css" href="css/datepicker.css" /> <link rel="stylesheet" href="css/datepicker.css" type="text/css" /> <link rel="stylesheet" media="screen" type="text/css" href="css/layout.css" /> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/datepicker.js"></script> <script type="text/javascript" src="js/eye.js"></script> <script type="text/javascript" src="js/utils.js"></script> <script type="text/javascript" src="js/layout.js?ver=1.0.2"></script> <script> $(document).ready(function() { $('#inputDate').val("<?php echo date('m/d/Y', $_GET['sd']) ?>"); }); </script> </head> <body> <?php include_once 'nav-menu.php' ?> <h2 style="text-align: center;">Work Order Information</h2> <div id="woInfo"> <fieldset> <?php include 'includes.php'; $postviewsql = "SELECT * FROM workorderstbl WHERE wo_id = :wo_id LIMIT 1"; $poststmt = $DBH->prepare($postviewsql); $poststmt->bindParam(':wo_id', $_GET['id'], PDO::PARAM_STR, 5); $poststmt->execute(); if(!($poststmt->rowCount())) { echo 'Work Order #' . $_GET['id'] . ' not found'; $DBH = null; } else { $row = $poststmt->fetch(PDO::FETCH_ASSOC); echo "<legend>Contact Info</legend>"; echo "<form id='wo_edit' action='wo_view.php' method='post'>"; echo "<a href='wo_view.php?id=" . $row['wo_id'] . "' style='float: left;'>Back to Service Request</a><br /><br />"; echo "<label for='wo_id'>Service Request: <strong><span id='wo_id'>" . $row['wo_id'] . "</span></strong>     Created On: <strong>" . date('F j<\s\up>S</\s\up>, Y', $row['date_posted']). "</strong>     Service Request Status: <select id='wo_status'><option value='" . $row['wo_status'] . "'>--" . $row['wo_status'] . "--</optino><option value='New'>New</option><option value='Scheduled'>Scheduled</option><option value='Pending'>Pending</option><option value='Closed'>Complete</option></select><br />"; echo "First Name: <input type='text' id='first_name' value='" . $row['first_name'] . "' />     Last Name: <input type='text' id='last_name' value='" . $row['last_name'] . "' /><br />"; echo "Phone Number: <input type='tel' id='phone_number' value='" . $row['phone_number'] . "' />     Email Address: <input type='email' id='email_addr' value='" . $row['email_addr'] . "' /></strong>"; echo "</fieldset><fieldset><legend>Address</legend>"; echo "Address Line 1: <input type='text' id='strt_addr' value='" . $row['strt_addr'] . "' /> Address Line 1: <input type='text' id='strt_addr2' value='" . $row['strt_addr2'] . "' /></br>"; echo "City: <input type='text' id='city' value='" . $row['city'] . "' /> State: <input type='text' id='state' value=' " . $row['state'] . "' /> Zip-Code: <input type='number' id='zip' value='" . $row['zip'] . "' /></strong><br />"; echo "</fieldset><fieldset><legend>Service Information:</legend>"; echo "Service Type: <select id='service_type'> <option value='" . $row['service_type'] . "'>--" . $row['service_type'] . "--</option><option value='Install'>Install</option><option value='Service Call'>Service Call</option><option value='Repair'>Repair</option></select>     Request Date: <strong>" . date('m-d-Y',$row['request_date']) . "</strong>     Requested Time Frame: <strong>" . $row['request_time'] . "</strong><br />"; echo "<label for='inputDate'>Service Date:</label><input class='inputDate' id='inputDate' name='inputDate' /><label id='closeOnSelect' style='display: none;'><input type='checkbox' checked='checked' /> Close on selection</label>     Service Time: <input type='time' value='" . $row['service_time'] . "' />"; echo "Service Notes:<br /><textarea id='service_notes' style='width: 100%;'>" . nl2br($row['service_notes']) . "</textarea><br />"; echo "<input type='submit' value='Update' /></form>"; $DBH = null; } ?> </fieldset> </div> </body> </html> I have been trying to fix this for the past couple days. I might be looking to hard at it, any help would be appreciated. Thank you! Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 21, 2014 Share Posted April 21, 2014 (edited) Your code is a little hard to read because of the way you are creating the HTML output.. I would propose a suggestion. Make the form a separate file that you include. Then, make that file such that it is mostly HTML and just change to PHP when outputting variables. So, instead of this unreadable mess echo "<label for='wo_id'>Service Request: <strong><span id='wo_id'>" . $row['wo_id'] . "</span></strong>     Created On: <strong>" . date('F j<\s\up>S</\s\up>, Y', $row['date_posted']). "</strong>     Service Request Status: <select id='wo_status'><option value='" . $row['wo_status'] . "'>--" . $row['wo_status'] . "--</optino><option value='New'>New</option><option value='Scheduled'>Scheduled</option><option value='Pending'>Pending</option><option value='Closed'>Complete</option></select><br />"; echo "First Name: <input type='text' id='first_name' value='" . $row['first_name'] . "' />     Last Name: <input type='text' id='last_name' value='" . $row['last_name'] . "' /><br />"; You would have this <label for='wo_id'> Service Request: <strong><span id='wo_id'><?php echo $row['wo_id']; ?></span></strong>     Created On: <strong><?php echo date('F j<\s\up>S</\s\up>, Y', $row['date_posted']); ?></strong>     Service Request Status: <select id='wo_status'> <option value='<?php echo $row['wo_status']; ?>'>--<?php echo $row['wo_status']; ?>--</optino> <option value='New'>New</option> <option value='Scheduled'>Scheduled</option> <option value='Pending'>Pending</option> <option value='Closed'>Complete</option> </select><br /> First Name: <input type='text' id='first_name' value='<?php echo $row['first_name']; ?>' />     Last Name: <input type='text' id='last_name' value='<?php echo $row['last_name']; ?>' /><br /> You'll notice you have an opening tag for the label - but there is no closing tag! There could be other such syntax errors that are causing problems with the form submission, but it is near impossible to "see" them in the current code. Also, you should not use this to determine if the user has submitted the form if(isset($_POST['submit'])) { . . . because it is only set if the user "clicks" on the submit button. A form can be submitted using the Enter key and that vallue may not be sent based upon the browser. A better method is to check the request method (or at least check for one of the form fields) if($_SERVER['REQUEST_METHOD']=="post") (Note sure if it should be "post" or "POST") So, my guess is that there are syntax problems in the form. But, you should at least verify what IS being sent to the processing page. Put this at the top of the script to see if anything is getting sent echo "POST data: <pre>" . print_r($_POST, 1) . "</pre>\n"; EDIT: Last tip. If you do need to create the output using multiple lines of echo statements you should include actual linebreaks in that output to make reading of the HTML easier.If you do this echo "Line 1 content<br>"; echo "Line 2 content<br>"; echo "Line 3 content<br>"; The HTML output would look like this Line 1 content<br>Line 2 content<br>Line 3 content<br> Not too hard to read with just that little bit of content. But, if it is something more complex like a form or table it can be very difficult to follow when trying to debug an issue. So, if you include linebreaks in the output like this (i.e. "\n") echo "Line 1 content<br>\n"; echo "Line 2 content<br>\n"; echo "Line 3 content<br>\n"; The output becomes much easier to read for errors Line 1 content<br> Line 2 content<br> Line 3 content<br> Edited April 21, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
travisco87 Posted April 21, 2014 Author Share Posted April 21, 2014 When creating the HTML file should I create it as conditional? What I mean by that is I also use the same layout almost when they submit the work order. Would I check for submission, if nothing is submitted insert the "placeholders"? Then if there is something submitted replace the placeholders with values instead? I am still trying to learn as I go, thank you so much for your insight. I will clean this up. Quote Link to comment Share on other sites More sharing options...
mogosselin Posted April 21, 2014 Share Posted April 21, 2014 What do you mean by "placeholder"? Do you mean the default text that is displayed in an input field? If yes, you can use the property "placeholder" in an input field to display default value. It won't be displayed if the value is not empty. For example: <input type="text" value="" placeholder="Default value"> If something exists in value="", then the value inside placeholder won't be displayed. Since you have labels besides your form, you don't need a place holder. Note : You could use the <label> tag for your form labels. The way it works is something like this: <label for="email">Your Email</label> <input id="email" value="" name="email" placeholder="email@company.com"> The "for" attribute of the "label" tag indicates the "id" of the tag the label is form (in this case, "email"). This is better for accessibility. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 21, 2014 Share Posted April 21, 2014 When creating the HTML file should I create it as conditional? What I mean by that is I also use the same layout almost when they submit the work order. Would I check for submission, if nothing is submitted insert the "placeholders"? Then if there is something submitted replace the placeholders with values instead? I am still trying to learn as I go, thank you so much for your insight. I will clean this up. To add to mogosselin's response, you would also put a value attribute in the field like so: <label for="email">Your Email</label> <input id="email" value="" name="email" placeholder="email@company.com" value="<?php echo $mail; ?>"> Now, to expand to that, here is what I would do: On the wo_edit.php page, I would remove the code to build the form and simply use an include(). $query = "SELECT * FROM workorderstbl WHERE wo_id = :wo_id LIMIT 1"; $poststmt = $DBH->prepare($query); $wo_id = intval($_GET['id']); $poststmt->bindParam(':wo_id', $wo_id, PDO::PARAM_STR, 5); $poststmt->execute(); if(!$poststmt->rowCount()) { echo 'Work Order #{$wo_id} not found'; $DBH = null; } else { $workOrder = $poststmt->fetch(PDO::FETCH_ASSOC); include('wo_form.php'); } Then on the wo_form.php page, I would define variables for the form at the top of the script based upon whether values were passed to the script or use empty values. Then include those variables in the form. Example: <?php $wo_id = isset($workOrder['wo_id']) ? htmlentities($workOrder['wo_id']) : ''; $date_posted = isset($workOrder['date_posted']) ? date('F j<\s\up>S</\s\up>, Y', $row['date_posted']) : ''; $wo_status = isset($workOrder['wo_status']) ? htmlentities($workOrder['wo_status']) : ''; $first_name = isset($workOrder['first_name']) ? htmlentities($workOrder['first_name']) : ''; $last_name = isset($workOrder['last_name']) ? htmlentities($workOrder['last_name']) : ''; $phone_number = isset($workOrder['phone_number']) ? htmlentities($workOrder['phone_number']) : ''; $email_addr = isset($workOrder['email_addr']) ? htmlentities($workOrder['email_addr']) : ''; $strt_addr = isset($workOrder['strt_addr']) ? htmlentities($workOrder['strt_addr']) : ''; $strt_addr2 = isset($workOrder['strt_addr2']) ? htmlentities($workOrder['strt_addr2']) : ''; $city = isset($workOrder['city']) ? htmlentities($workOrder['city']) : ''; $state = isset($workOrder['state']) ? htmlentities($workOrder['state']) : ''; $zip = isset($workOrder['zip']) ? htmlentities($workOrder['zip']) : ''; $service_type = isset($workOrder['service_type']) ? htmlentities($workOrder['service_type']) : ''; $request_date = isset($workOrder['m-d-Y']) ? date('F j<\s\up>S</\s\up>, Y', $row['request_date']) : ''; $request_time = isset($workOrder['request_time']) ? htmlentities($workOrder['request_time']) : ''; $service_time = isset($workOrder['service_time']) ? htmlentities($workOrder['service_time']) : ''; $service_notes = isset($service_notes['zip']) ? nl2br(htmlentities($service_notes['zip'])) : ''; ?> <legend>Contact Info</legend> <form id='wo_edit' action='wo_view.php' method='post'> <a href="wo_view.php?id=<?php echo $wo_id; ?>" style='float: left;'>Back to Service Request</a><br /><br /> Service Request: <strong><span id='wo_id'><?php echo $wo_id; ?></span></strong>     Created On: <strong><?php echo $date_posted; ?></strong>     Service Request Status: <select id='wo_status'> <option value='<?php echo $wo_status; ?>'>--<?php echo $wo_status; ?>--</optino> <option value='New'>New</option> <option value='Scheduled'>Scheduled</option> <option value='Pending'>Pending</option> <option value='Closed'>Complete</option> </select><br /> First Name: <input type='text' id='first_name' value='<?php echo $first_name; ?>' />     Last Name: <input type='text' id='last_name' value='<?php echo $last_name; ?>' /><br /> Phone Number: <input type='tel' id='phone_number' value='<?php echo $phone_number; ?>' />     Email Address: <input type='email' id='email_addr' value='<?php echo $email_addr; ?>' /></strong> <fieldset> <legend>Address</legend> Address Line 1: <input type='text' id='strt_addr' value='<?php echo $strt_addr; ?>' /> Address Line 2: <input type='text' id='strt_addr2' value='<?php echo $strt_addr2; ?>' /></br> City: <input type='text' id='city' value='<?php echo $city; ?>' /> State: <input type='text' id='state' value='<?php echo $state; ?>' /> Zip-Code: <input type='number' id='zip' value='<?php echo $zip; ?>' /></strong><br /> </fieldset> <fieldset> <legend>Service Information:</legend> Service Type: <select id='service_type'> <option value='<?php echo $service_type; ?>'>--<?php echo $service_type; ?>--</option> <option value='Install'>Install</option> <option value='Service Call'>Service Call</option> <option value='Repair'>Repair</option> </select>     Request Date: <strong><?php echo $request_date; ?></strong>     Requested Time Frame: <strong><?php echo $request_time; ?></strong><br /> <label for='inputDate'>Service Date:</label> <input class='inputDate' id='inputDate' name='inputDate' /> <input type='checkbox' checked='checked' /> <label id='closeOnSelect' style='display: none;'>Close on selection</label>     Service Time: <input type='time' value='<?php echo $service_time; ?>' /> Service Notes:<br /> <textarea id='service_notes' style='width: 100%;'><?php echo service_notes; ?></textarea><br /> <input type='submit' value='Update' /> </fieldset> </form> 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.