Jump to content

Recommended Posts

Let me start by apologizing ahead of time for my complete and utter lack of knowledge with respect to PHP coding.

I've been tasked with creating a timesheet that office employees would fill out at the end of each day for time tracking purposes.  This form would then be sent electronically to be input into our accounting software.

The form is as follows:

 

th_Screenshot_zps355ed0eb.png

 

I've used a little javascript to validate the form to ensure certain fields have been entered correctly.  If everything checks out, the PHP script is supposed to kick in and email the contents (of only filled in fields) to the desired address.

 

My goal here is to to have the end message looks something like this:

 

Name: user who submitted form

Date: date the form was submitted

 

Job Number: ####-## -- Description -- hours worked on job hrs -- OT hours worked on job hrs -- DT hours worked on job hrs

Job Number: ####-## -- Description -- hours worked on job hrs -- OT hours worked on job hrs -- DT hours worked on job hrs

etc.

 

...but, if fields are blank, I would like to have them omitted.  That is to say, if no OT/DT was worked on the first job, don't send that field in the message.  Or, if only one job was worked that day, don't send the other seven rows.

 

This is what I've got so far:

 

$replyemail="accounting email";
$thesubject = "Daily Timesheet Submission";
$yourname = $_POST["yourname"];
$date = $_POST["date"];

$jb1 = $_POST["jb1"];
$ds1 = $_POST["ds1"];
$st1 = $_POST["st1"];
$ot1 = $_POST["ot1"];
$dt1 = $_POST["dt1"];
<!---SNIP--->
$jb8 = $_POST["jb8"];
$ds8 = $_POST["ds8"];
$st8 = $_POST["st8"];
$ot8 = $_POST["ot8"];
$dt8 = $_POST["dt8"];

$themessage = "Name: $yourname \nDate: $date
			\nJob: $jb1 - $ds1 - $st1 hrs - $ot1 hrs - $dt1 hrs
			<!---SNIP--->
			\nJob: $jb8 - $ds8 - $st8 hrs - $ot8 hrs - $dt8 hrs";
mail("$replyemail",
     "$thesubject",
     "$themessage",
     "From: $yourname\nReply-To: $yourname");

header("Location: index.html");
exit();

 

It sends the message just fine, but the Job: - - hrs - hrs - hrs displays for all eight rows.

I've found this little bit of code on this site (thanks gizmola):

 

$subject = 'Daily Timesheet Submission';
$replyemail = 'accounting email';
$message = "$date timesheet for $yourname\n\n"; 
foreach ($_POST as $key => $value) {
$value = trim(strip_tags($value));
	if (!empty($value)) {
		$message .= "$value\n";
	}
}

mail("$replyemail",
     "$subject",
     "$message",
     "From: $yourname\nReply-To: $yourname");

header("Location: index.html");
exit();

 

But it pushes all data onto a new line.  I'd like to keep a similar layout within the email as is shown in the form itself.

 

I feel this is an easy problem to solve; however, my lack of knowledge leaves me feeling a little embarrassed.

Any help or insight into this matter would be greatly appreciated.

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/268629-ignore-empty-fields-in-php-form-mailer/
Share on other sites

You need to start by renaming your fields so they are "logically" associated into arrays. Then you can easily create code to process the data without a lot of work. Trying to explain how to do this would take more time than just showing you. Here is a working script using a form just like the one you showed that dynamically creates the form and processes it. Try it out and then implement as you need it.

 

<?php

if(isset($_POST['name']))
{
    //Include separate php script to process the form
    //Putting here only for brevity
    $email .= "\n\nName: {$_POST['name']}\n";
    $email .= "Date: {$_POST['date']}\n";
    foreach($_POST['time'] as $time)
    {
        //Assuming Job No is required, only process records were job not empty
        $jobNo = trim($time['job_no']);
        if(!empty($jobNo))
        {
            $desc = trim($time['desc']);
            $st = intval($time['st']);
            $ot = intval($time['ot']);
            $dt = intval($time['dt']);
            $email .= "\nJob Number: {$jobNo} -- {$desc} -- {$st} -- {$ot} -- {$dt}";
        }
    }
    echo "<pre>{$email}</pre>";
    echo "<a href=''>Go back to form</a>";
    exit();
}

//Create the duplicated form fields
$records = 8;
$formFieldsHTML = '';
for($i=0; $i<$records; $i++)
{
    $formFieldsHTML .= "<tr>\n";
    $formFieldsHTML .= "<td><input type='text' name='time[$i][job_no]' class='job_no' /></td>\n";
    $formFieldsHTML .= "<td><input type='text' name='time[$i][desc]' class='desc' /></td>\n";
    $formFieldsHTML .= "<td><input type='text' name='time[$i][st]' class='st' /></td>\n";
    $formFieldsHTML .= "<td><input type='text' name='time[$i][ot]' class='ot' /></td>\n";
    $formFieldsHTML .= "<td><input type='text' name='time[$i][dt]' class='dt' /></td>\n";
    $formFieldsHTML .= "</tr>\n";
}


?>
<html>
<style>

label, th { font-weight: bold; color: blue; }
.job_no { width: 100px; }
.desc { width: 300px; }
.st, .ot, .dt { width: 40px; }

</style>

<body>
<form action="" method="post">

    <div style="float:left;">
    <label for="name">NAME</lablel><br>
    <input type="text" name="name" id="name" />
    </div>

    <div>
    <label for="date">DATE</lablel><br>
    <input type="text" name="date" id="date"/>
    </div>
    <br>

    <table border="1" id="timesheet">
      <tr>
        <th>JOB NO.</th>
        <th>DESCRIPTION</th>
        <th>ST</th>
        <th>OT</th>
        <th>DT</th>
      </tr>
      <?php echo $formFieldsHTML; ?>
    </table>
    <button type="submit">Submit timesheet</button>
    <button type="reset">Reset timesheet</button>

</form>

</body>
</html>

You also need to secure the $_POST[''];'s, you mailer isn't secure & people will use it to spam. You also want to strip out and mail forward commands CC: BCC:, stop the email being sent to other email addresses they throw into the field

Thanks for the quick responses guys!

 

@Psycho: I'll try to implement what you've posted and post back with the results.  I'm just leaving work now so it may not be until tomorrow before I can have something to test.

@coded4u: Ideally, we would run this internally over our server (assuming it supports PHP :confused:), but I'll have to look into securing it for sure.  Thanks for the heads up.

 

Cheers.

Spoiledgoods: You want to secure the code regardless of where it's meant to be running, as you can never trust the users 100%. Even if they themselves have no malicious intent, there are plenty of people who have and can hijack their computers. Not to mention the fact accidents do happen, and it's always a good idea to safe guard yourself against them.

The simple way would be use empty or a string comparison.

if(!empty($_POST['foo'])) {
// do something
}

// or

if(strlen($_POST['foo']) > 0) {
// do something
}

 

You can also use some comparison operators to compare other values like integers, exact matches, and etc.

Well, I've managed to integrate what Psycho had posted into my existing form, and today all of the testing worked well. I haven't fully secured the form as I'm still trying to wrap my head around this bit, but it has not yet gone "live" so I still have some time.

 

Thanks again everyone for pointing me in the right direction.

Edited by spoiledgoods
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.