vschiano2008 Posted August 5, 2012 Share Posted August 5, 2012 Hi, I am building what I had hoped to be a basic website for employees to submit a detailed timesheet and have it email me. I have built the form and a functioning email script, which I am pretty happy with. I have only one complaint for now - There are a series of form rows for multiple employees to be listed on the timesheet. I built some dropdowns to select start and end time. What I am looking to do is basically have the output email ignore a set of fields if "name" is blank. <body> </body> </html> <?php $Date = $_REQUEST['Date']; $JobNum = $_REQUEST['JobNum']; $JobType = $_REQUEST['JobType']; $Inspector = $_REQUEST['Inspector']; $OnStreet = $_REQUEST['OnStreet']; $CrsStreet1= $_REQUEST['CrsStreet1']; $CrsStreet2= $_REQUEST['CrsStreet2']; $Desc= $_REQUEST['Desc']; $first1= $_REQUEST['first1']; $last1= $_REQUEST['last1']; $start1= $_REQUEST['start1']; $ampm11= $_REQUEST['ampm11']; $end1= $_REQUEST['end1']; $ampm12= $_REQUEST['ampm12']; $first2= $_REQUEST['first2']; $last2= $_REQUEST['last2']; $start2= $_REQUEST['start2']; $ampm21= $_REQUEST['ampm21']; $end2= $_REQUEST['end2']; $ampm22= $_REQUEST['ampm22']; $first3= $_REQUEST['first3']; $last3= $_REQUEST['last3']; $start3= $_REQUEST['start3']; $ampm31= $_REQUEST['ampm31']; $end3= $_REQUEST['end3']; $ampm32= $_REQUEST['ampm32']; $first4= $_REQUEST['first4']; $last4= $_REQUEST['last4']; $start4= $_REQUEST['start4']; $ampm41= $_REQUEST['ampm41']; $end4= $_REQUEST['end4']; $ampm42= $_REQUEST['ampm42']; $first5= $_REQUEST['first5']; $last5= $_REQUEST['last5']; $start5= $_REQUEST['start5']; $ampm51= $_REQUEST['ampm51']; $end5= $_REQUEST['end5']; $ampm52= $_REQUEST['ampm52']; $first6= $_REQUEST['first6']; $last6= $_REQUEST['last6']; $start6= $_REQUEST['start6']; $ampm61= $_REQUEST['ampm61']; $end6= $_REQUEST['end6']; $ampm62= $_REQUEST['ampm62']; $first7= $_REQUEST['first7']; $last7= $_REQUEST['last7']; $start7= $_REQUEST['start7']; $ampm71= $_REQUEST['ampm71']; $end7= $_REQUEST['end7']; $ampm72= $_REQUEST['ampm72']; $first8= $_REQUEST['first8']; $last8= $_REQUEST['last8']; $start8= $_REQUEST['start8']; $ampm81= $_REQUEST['ampm81']; $end8= $_REQUEST['end8']; $ampm82= $_REQUEST['ampm82']; $first9= $_REQUEST['first9']; $last9= $_REQUEST['last9']; $start9= $_REQUEST['start9']; $ampm91= $_REQUEST['ampm91']; $end9= $_REQUEST['end9']; $ampm92= $_REQUEST['ampm92']; $first10= $_REQUEST['first10']; $last10= $_REQUEST['last10']; $start10= $_REQUEST['start10']; $ampm101= $_REQUEST['ampm101']; $end10= $_REQUEST['end10']; $ampm102= $_REQUEST['ampm102']; $first11= $_REQUEST['first11']; $last11= $_REQUEST['last11']; $start11= $_REQUEST['start11']; $ampm111= $_REQUEST['ampm111']; $end11= $_REQUEST['end11']; $ampm112= $_REQUEST['ampm112']; $first12= $_REQUEST['first12']; $last12= $_REQUEST['last12']; $start12= $_REQUEST['start12']; $ampm121= $_REQUEST['ampm121']; $end12= $_REQUEST['end12']; $ampm122= $_REQUEST['ampm122']; $EquipNum1= $_REQUEST['EquipNum1']; $EquipTyp1= $_REQUEST['EquipTyp1']; $EquipNum2= $_REQUEST['EquipNum2']; $EquipTyp2= $_REQUEST['EquipTyp2']; $EquipNum3= $_REQUEST['EquipNum3']; $EquipTyp3= $_REQUEST['EquipTyp3']; $EquipNum4= $_REQUEST['EquipNum4']; $EquipTyp4= $_REQUEST['EquipTyp4']; $EquipNum5= $_REQUEST['EquipNum5']; $EquipTyp5= $_REQUEST['EquipTyp5']; $EquipNum6= $_REQUEST['EquipNum6']; $EquipTyp6= $_REQUEST['EquipTyp6']; $EquipNum7= $_REQUEST['EquipNum7']; $EquipTyp7= $_REQUEST['EquipTyp7']; $MatTyp1= $_REQUEST['MatTyp1']; $MatQty1= $_REQUEST['MatQty1']; $MatTyp2= $_REQUEST['MatTyp2']; $MatQty2= $_REQUEST['MatQty2']; $MatTyp3= $_REQUEST['MatTyp3']; $MatQty3= $_REQUEST['MatQty3']; $MatTyp4= $_REQUEST['MatTyp4']; $MatQty4= $_REQUEST['MatQty4']; $MatTyp5= $_REQUEST['MatTyp5']; $MatQty5= $_REQUEST['MatQty5']; $SubTyp1= $_REQUEST['SubTyp1']; $SubName1= $_REQUEST['SubName1']; $SubHrs1= $_REQUEST['SubHrs1']; $SubTyp2= $_REQUEST['SubTyp2']; $SubName2= $_REQUEST['SubName2']; $SubHrs2= $_REQUEST['SubHrs2']; $SubTyp3= $_REQUEST['SubTyp3']; $SubName3= $_REQUEST['SubName3']; $SubHrs3= $_REQUEST['SubHrs3']; $message = "Date: $Date\n JobNumber: $JobNum\n JobType: $JobType\n Inspector: $Inspector\n On Street: $OnStreet\n Cross Street: $CrsStreet1\n Cross Street: $CrsStreet2\n Work Description: $Desc\n Labor Emp#1: $first1 $last1 -$start1 $ampm11 to $end1 $ampm12 Emp#2: $first2 $last2 -$start2 $ampm21 to $end2 $ampm22 Emp#3: $first3 $last3 -$start3 $ampm31 to $end3 $ampm32 Emp#4: $first4 $last4 -$start4 $ampm41 to $end4 $ampm42 Emp#5: $first5 $last5 -$start5 $ampm51 to $end5 $ampm52 Emp#6: $first6 $last6 -$start6 $ampm61 to $end6 $ampm62 Emp#7: $first7 $last7 -$start7 $ampm71 to $end7 $ampm72 Emp#8: $first8 $last8 -$start8 $ampm81 to $end8 $ampm82 Emp#9: $first9 $last9 -$start9 $ampm91 to $end9 $ampm92 Emp#10: $first10 $last10 -$start10 $ampm101 to $end10 $ampm102 Emp#11: $first11 $last11 -$start11 $ampm111 to $end11 $ampm112 Emp#12: $first12 $last12 -$start12 $ampm121 to $end12 $ampm122\n Equipment $EquipNum1 $EquipTyp1 $EquipNum2 $EquipTyp2 $EquipNum3 $EquipTyp3 $EquipNum4 $EquipTyp4 $EquipNum5 $EquipTyp5 $EquipNum6 $EquipTyp6 $EquipNum7 $EquipTyp7\n Material $MatTyp1 -$MatQty1 $MatTyp2 -$MatQty2 $MatTyp3 -$MatQty3 $MatTyp4 -$MatQty4 $MatTyp5 -$MatQty5\n Subcontractor $SubName1 $SubTyp1 - $SubHrs1 $SubName2 $SubTyp2 - $SubHrs2 $SubName3 $SubTyp3 - $SubHrs3"; if (!isset($_REQUEST['Date'])) { header( "Location: http://www.eruptiontaxservice.com/FDC.html" ); } elseif (empty($Date) || empty($OnStreet)) { header( "Location: http://www.eruptiontaxservice.com/error.html" ); } else { mail( "smartguyswin@yahoo.com", "FDC Entry Received", $message); header( "Location: http://www.eruptiontaxservice.com/thankyou.html" ); } ?> Bringing your attention to the $message area: Basically, if Emp#2's name is blank, I want it to ignore the rest of the line, rather than submit the pre-set times in "Start1" "ampm11" "end1" "ampm12. I'm getting emails that just say Emp#2 7:00AM to 3:30PM, but since they don't have a name, the time shouldn't display. I also have to do this with many other lines, the idea was to leave them enough form fields but it's respective quantity field was left blank, to just ignore it in the first place. I am an accountant, not a web designer or programmer, I've been able to build the forms with basic html knowledge, dreamweaver and google. I wrote the php script based on a few different tutorials and trying to teach myself as I go. I am not really familiar with different functions or anywhere near all of my options. I've seen things involving post, echo and some other terms I am not familiar with, I'd also appreciate any good links you might want to share. I didn't intend to make a major project out of this, I know it is relatively basic for an experienced person and certainly do-able for a beginner. Willing to trade management consulting services for a great explanation on how to finish this project up. Thank you! the form is live at www.eruptiontaxservice.com/FDC.html it is an old domain i am using to test. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 5, 2012 Share Posted August 5, 2012 OK, you can solve your problem AND greatly reduce the complexity and size of your code at the same time. Right now you have a lot of hard-coded field names. Instead, make your field names more programmatic. You are making this way harder than it should be. So, instead of having name fields such as <input type="text" name="first1"> <input type="text" name="last1"> <input type="text" name="first2"> <input type="text" name="last2"> <input type="text" name="first3"> <input type="text" name="last3"> Create your fields as arrays such as <input type="text" name="emp[1][first]"> <input type="text" name="emp[1][last]"> <input type="text" name="emp[2][first]"> <input type="text" name="emp[2][last]"> [code]<input type="text" name="emp[3][first]"> <input type="text" name="emp[3][last]"> Now you can do two things: 1. You can create your form programatically and you can process it programatically. In the processing code you could do somethign such as this foreach($_POST['emp'] as $empIndex => $employeeData) { //Process each employee separately } By writing all the same logic to process each employee the same you make it very possible of having errors that are difficult to find. by creating a loop to process each record you can reuse the same code. It will also make it easy to skip any records that have an empty value. Quote Link to comment Share on other sites More sharing options...
vschiano2008 Posted August 5, 2012 Author Share Posted August 5, 2012 Thank you for the fast response. How would that change my script? Do I delete the $_Request lines as well as the respective lines within $message? I am a bit lost with your explanation still, does that one line of code replace all of the emp# lines? I am able to change the html, i see how it makes one variable "emp" and then numbers them [1] and indicates multiple fields for each emp #, ie [first][last]etc. I definitely realized that the way i wrote it is very inefficient, but logically it was easiest for me to make sense of, using all of the hard coding. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 5, 2012 Share Posted August 5, 2012 If you implement your fields in that manner, then you will use a foreach() loop to process the individual field sets for the employees. By hardcoding everything you are making the code much more difficult to maintain and are drastically increasing the likelihood of introducing bugs that are going to be harder to find. If you use a loop to process different sets of records you are assured that if the loop is working correctly for one set it is working the same for all sets. Your form is a flat HTML file that is over 1,000 lines long! By creating loops to generate the field sets and the select options you could reduce the code to well under 25% of it's current size and it will be more flexible and scalable. Let me review your form and see what I can come up with. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 6, 2012 Share Posted August 6, 2012 OK, you need to start with modifying your form to make this work efficiently. Here was a quick rewrite of your form which was written as a flat HTML page. It was over 1,000 lines. The rewrite below creates the form exactly as you had it before except that it does it programatically and takes less than 200 lines of code. Note, I did make one change. For the start/end times I used single select lists for the time (including the AM/PM). having the user click two fields to enter a time is a waste and it makes processing that much more difficult. So, try this code out and look at what it is doing. I would also put the arrays to define the select lists into a separate file which is included. That way you can use the same arrays on the processing page. When I get some more time I go through your processing logic. <?php //Function to create options lists from an array function createOptions($values, $selectedValue) { $optionsHTML = ''; foreach($values as $value) { $selected = ($value==$selectedValue) ? ' selected="selected"': ''; $optionsHTML .= "<option value='{$value}'{$selected}>{$value}</option>\n"; } return $optionsHTML; } $startTimeOptions = ''; $endTimeOptions = ''; for($time=0; $time<24; $time+=.5) { $hour = ($time%12==0) ? '12' : $time%12; $min = ($time==ceil($time)) ? '00' : '30'; $ampm = ($time<12) ? 'AM' : 'PM'; $timeStr = "{$hour}:{$min} {$ampm}"; $startSelected = ($timeStr=='7:00 AM') ? ' selected="selected"': ''; $startTimeOptions .= "<option value='{$timeStr}'{$startSelected}>{$timeStr}</option>\n"; $endSelected = ($timeStr=='3:30 PM') ? ' selected="selected"': ''; $endTimeOptions .= "<option value='{$timeStr}'{$endSelected}>{$timeStr}</option>\n"; } $equipList = array('Backhoe', '6 Wheel Dump', 'Van/Truck', 'Compressor', 'HDD', 'Trailer', 'Road Saw'); $materialList = array('Asphalt Base', 'Asphalt Top', 'Sand', 'Fill', 'Stone', 'Debris Removed', 'Cold Patch', 'Poured Concrete', 'Cement Bags'); $subTypeList = array('Dump Truck', 'Vac Truck', 'Boom Truck', 'MPT', 'Other'); ?> <!DOCTYPE HTML> <html><!-- InstanceBegin template="/Templates/DWcourseMultiscreen.dwt" codeOutsideHTMLIsLocked="false" --> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width"> <!-- InstanceBeginEditable name="doctitle" --> <title>Field Data Capture</title> <!-- InstanceEndEditable --> <link href="file:///F|/Users/Victor/Downloads/assets/thrColHFmulti.css" rel="stylesheet" type="text/css"> <!-- Phone --> <link href="file:///F|/Users/Victor/Downloads/assets/phone.css" rel="stylesheet" type="text/css" media="only screen and (max-width:480px)"> <!-- Tablet --> <link href="file:///F|/Users/Victor/Downloads/assets/tablet.css" rel="stylesheet" type="text/css" media="only screen and (min-width:481px) and (max-width:800px)"> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> </head> <body> <div class="content"><!-- InstanceBeginEditable name="content" --> <body bgcolor="#FFFFFF"> <h1>Field Data Capture</h1> <p> </p> <form action="EmailForm.php" method="post" name="EmailForm"> <table width="100%" border="1"> <tr> <td>Date</td> <td><input type="text" name="Date"></td> </tr> <tr> <td>Job Number</td> <td><input type="text" name="JobNum"></td> </tr> <tr> <td>Job Type</td> <td> <input type="text" name="JobType"></td> </tr> <tr> <td>Inspector</td> <td><input type="text" name="Inspector"></td> </tr> <tr> <td>On Street</td> <td><input type="text" name="OnStreet"></td> </tr> <tr> <td>Cross Street 1</td> <td><input type="text" name="CrsSreet1"></td> </tr> <tr> <td>Cross Street 2</td> <td><input type="text" name="CrsStreet2"></td> </tr> <tr> <td>Work Description</td> <td><textarea name="Desc" cols="50" rows="5"></textarea></td> </tr> </table> <table width="100%" border="1"> <tr> <th scope="col">First</th> <th scope="col">Last</th> <th scope="col">Start</th> <th scope="col">End</th> </tr> <?php //Create input fields for employee time entry $employeeCount = 12; for($empIdx=0; $empIdx<$employeeCount ; $empIdx++) { echo "<tr>\n"; echo "<td><input name='emp[{$empIdx}][first]' type='text'></td>\n"; echo "<td><input name='emp[{$empIdx}][last]' type='text'></td>\n"; echo "<td><select name='emp[{$empIdx}][start]'>{$startTimeOptions}</select></td>\n"; echo "<td><select name='emp[{$empIdx}][end]'>{$endTimeOptions}</select></td>\n"; echo "</tr>\n"; } ?> </table> <table width="100%" border="1"> <tr> <th scope="Equip#">Equip #</th> <th scope="EquipType">EquipType</th> </tr> <?php //Create input fields for equipment usage entry $selectedEquipment = array('Backhoe', '6 Wheel Dump', 'Van/Truck', 'Van/Truck', 'Compressor', 'Trailer', 'Road Saw'); foreach($selectedEquipment as $equipIdx => $equipment) { echo "<tr>\n"; echo "<td><input name='equip[{$equipIdx}][num]' type='text'></td>\n"; echo "<td><select name='equip[{$equipIdx}][type]'>\n"; echo createOptions($equipList, $equipment); echo "</select></td>\n"; echo "</tr>\n"; } ?> </table> <table width="100%" border="1"> <tr> <th scope="col">Material</th> <th scope="col">Qty</th> </tr> <?php //Create input fields for material usage entry $selectedMaterial = array('Debris Removed', 'Sand', 'Asphalt Base', 'Cold Patch', 'Cement Bags'); foreach($selectedMaterial as $materialIdx => $material) { echo "<tr>\n"; echo "<td><select name='material[{$materialIdx}][qty]'>\n"; echo createOptions($materialList, $material); echo "</select></td>\n"; echo "<td><input name='material[{$materialIdx}][type]' type='text'></td>\n"; echo "</tr>\n"; } ?> </table> <table width="100%" border="1"> <tr> <th scope="col">Subcontractor Type</th> <th scope="col">Vendor Name</th> <th scope="col">Hours</th> </tr> <?php //Create input fields for subcaontractor entry $selectedSubType = array('Dump Truck', 'Vac Truck', 'Other'); foreach($selectedSubType as $subTypeIdx => $subType) { echo "<tr>\n"; echo "<td><select name='sub[{$subTypeIdx}][type]'>\n"; echo createOptions($subTypeList, $subType); echo "</select></td>\n"; echo "<td><input name='sub[{$subTypeIdx}][name]' type='text'></td>\n"; echo "<td><input name='sub[{$subTypeIdx}][hours]' type='text'></td>\n"; echo "</tr>\n"; } ?> </table> <input type="Submit" name="Submit" value="Submit"> </form> <!-- end .content --> <p><br> </p> <!-- InstanceEndEditable --><!-- end .content --></div> <div class="footer"> <p>Network Infrastructure Inc., All Rights Reserved</p> <!-- end .footer --></div> <!-- end .container --></div> </body> <!-- InstanceEnd --></html> Quote Link to comment Share on other sites More sharing options...
hakimserwa Posted August 6, 2012 Share Posted August 6, 2012 php is one way of eliminating reputation so why dont you use it in conjunction with mysql so to reduce this kind of reputation Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 6, 2012 Share Posted August 6, 2012 php is one way of eliminating reputation so why dont you use it in conjunction with mysql so to reduce this kind of reputation I believe you meant to say "repetition" not "reputation". Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 6, 2012 Share Posted August 6, 2012 OK, I enjoy these types of problems and wanted to provide some best practices to you. But trying to explain everything through a forum would take ages. So I went ahead and rewrote the form and the processing logic. Everything is pretty much driven by a set of arrays. So, you can add/remove options in any of the select list or add/remove additional rows in the form just by editing an array. Also, when processing the data, records with no appropriate values are excluded: - Employee records must have a first or last name - Equipment must have a number - Materials must have a QTY - Sub Contractors must have a Name or Hours I expect that you may want to tweak some of that logic. For example, you may want to reject Sub Contractors records unless they have a name. I.e. If only hours are entered exclude them. Or you may want to make those validation errors that prevent the form from submitting. So, if there is a Sub Contractor record with hours entered but no name, create an error and send back to the user. I'll leave that for you to implement. There are three files attached: FDC.php This is the main file. It will determine if the form was posted. If so it will perform any validations. If validations pass it will send the email. If there are errors or the form was not posted, the form script will be included. And, if there were errors an error message will be displayed at the top of the form AND the form fields will be sticky - i.e. any data the user entered will be populated so they don't have to re-input all the data. fdc_form.php This is the script to display the form. There are arrays right before each select field to determine how many sets of input to display and the defaults for the select fields. Just modify these arrays to add/remove as needed. lookup_data.php This script contains arrays of the values to use for each of the select fields. If you need to modify the available values, just edit these arrays. 18786_.php 18787_.php 18788_.php 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.