wantabe2
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?
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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.
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>
You might find this book helpful. It has great examples and is very readable.
http://www.sitepoint.com/books/phpmysql4/
http://www.sitepoint.com/books/phpmysql4/
ASKER
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
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
<input type="text" name="order_date" size="35" onchange="populateAssigned
<input type="text" name="assgn_date" id="assign_date" size="35" />
Open in new window