Link to home
Start Free TrialLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

Auto Populate Date Field

I attached the code in question. If you pull it up in a browser you will see the last two fields named order date & assign date.  My question is, if someone enters a date of 2/7/2011 in the order date box then hits the tab key to go to the next field, a date of 2/14/2011 will automatically get populated into the assign date field.

Basically, anything that is entered into the date field, then in the assign date box, a date 7 days in the future will appear. Is this possible?
<html>

<head>
<title> Update Database </title>
</head>

<body>
<form method="post" action="add.php">

<b>First Name:</b> <br />

<input type="text" name="customer_fname" size="35" /><br />

<b>Last Name:</b> <br />

<input type="text" name="customer_lname" size="35" /><br />

<b>Location:</b> <br />

<input type="text" name="location" size="35" /><br />

<b>Order Date:</b> <br />

<input type="text" name="order_date" size="35" /><br />

<b>Assign Date:</b> <br />

<input type="text" name="assgn_date" size="35" /><br />

<input type="submit" value="Submit" />

</form>
</body>
</html>

Open in new window

Avatar of ChetOS82
ChetOS82
Flag of United States of America image

Put an onchange event on the order_date, and give assign_date an id.  Then use the script below (this is off the top of my head, some debugging will be required):

<input type="text" name="order_date" size="35" onchange="populateAssignedDate(this);" />
<input type="text" name="assgn_date" id="assign_date" size="35" />

<script type="text/javascript">
function populateAssignedDate(order_date_input) {
  // Convert this date to a Date object.
  var splitOn = '';
  if (order_date_input.indexOf("/") != -1) {
     splitOn = '/';
  } else if (order_date_input.indexOf("-")) {
    splitOn = '-';
  }

  if (splitOn === '') {
    alert("Error: invalid date delimiter.  Valid delimiters are forward slash and dash.");
    return;
  }

  // Assume date format: MM/DD/YYYY
  var dateParts = order_date_input.value.split(splitOn);
  var newDate = new Date((dateParts[0] - 1), dateParts[1], dateParts[2]);
  newDate.setDate(newDate.getDate() + 7);

   // Get the assign_date text box.
  var assigned_date = document.getElementById('assign_date');
  assign_date.value = (newDate.getMonth() + 1) + splitOn + newDate.getDate() + splitOn + newDate.getFullYear();
}
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of daveamour
daveamour
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is the user-experience that you want to achieve?  Is the user to be allowed to change the assign date?  If so you might not bother filling in the field - just tell them that unless they change it, the assign date will be seven days after the order date.  If the user is not allowed to change it, why put it on the screen at all?

You will still have to validate all of this in the server side script.  In there you will use strtotime() to turn the order date into a UNIX timestamp.  It's fairly easy to add seven days to a UNIX timestamp, something like this:

$week_later = $now_timestamp + 60*60*24*7;

Some more information on handling DATETIME information is available here:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Avatar of wantabe2

ASKER

This code is going to part of a work flow system I'm trying to set up. At this time, the data is going to get written to a mysql database. The attached code is what I have so far. I have several other date fields I will type in later but I just put in two dates to help keep the confusion down.

Basically, all dates will be based off of the order date. Once the order date is typed in my the user & they hit the tab key, I'd like the other dates to be automatically typed in in the format year-month-day. I'm very new to PHP & HTML so please excuse my ignorance. If I could get the code to make the "due date" field auto populate I think I can figure out the rest of it. In the code below. If a user types in 2011-2-7 in the "order date" field then tabs to the next field, I'd like the date 2011-2-14 to be automatically typed in the "due date" field.  I've attached the code again so hopfully this will help someone figure it out.
<html>

<head>
<title> Update Database </title>
</head>

<body>
<form method="post" action="add.php">

<b>First Name:</b> <br />

<input type="text" name="customer_fname" size="35" /><br />

<b>Last Name:</b> <br />

<input type="text" name="customer_lname" size="35" /><br />

<b>Location:</b> <br />

<input type="text" name="location" size="35" /><br />

<b>Order Date:</b> <br />

<input type="text" name="order_date" size="35" /><br />

<b>Due Date:</b> <br />

<input type="text" name="due_date" size="35" /><br />

<input type="submit" value="Submit" />

</form>
</body>
</html>

Open in new window

You might find this book helpful.  It has great examples and is very readable.  
http://www.sitepoint.com/books/phpmysql4/
daveamour,
The code you provided come closest to working for me than anything. Instead of getting a date in the assign date field I get the text NaN. I've attached a screenshot below. Thanks again for your help.
nan.JPG
Thats becasue the date you have entered is year first and I was assuming year last so just swap the code around.  Validating dates entered as free text can be very difficult because of these differences so maybe its best top use a calendar control?
ps - NaN = Not a number