TapeGun007 Posted January 20, 2017 Share Posted January 20, 2017 I'm just curious if there's a much better way to do this: I have a form and the input looks like this: <td><input name="InspectionExp" value="<?=$InspectionExp; ?>" id="date4" placeholder="mm-dd-yyyy" /></td> The id on the form is for a jquery that auto formats the input field when selected to "__-__-____". By default a new user InspectionExp is a DATE field in mySQL and is set to NULL. So when the form is initially loaded up, the INPUT field above is blank with the place holder showing. So first I check if(isset($_POST['LicenseExp']) { Then for whatever reason this seems to be the only thing that works right, but seems really sloppy to me: if(!strlen($LicenseExp) == 10){ $LicenseExp = DateTime::createFromFormat('m-d-Y', $_POST['LicenseExp'])->format('Y-m-d'); } If I don't have an IF statement, then by default it's going to UPDATE the database with a 1969-12-31 date on new users. I can't use isset because the form makes the value set (again only on new users). I mean, the code works yes, but I am pretty certain one of you gurus could show me a better way. Quote Link to comment Share on other sites More sharing options...
TapeGun007 Posted January 20, 2017 Author Share Posted January 20, 2017 (edited) Whoops... I thought it worked. but when submitting the form with nothing in the INPUT field it crashes on the if(!strlen($PostLicenseExp) == 10){ echo "yes"; $PostLicenseExp = DateTime::createFromFormat('m-d-Y', $_POST['LicenseExp'])->format('Y-m-d'); } I see... I didn't need the ! before strlen...I had it the wrong way around. Edited January 20, 2017 by TapeGun007 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted January 20, 2017 Share Posted January 20, 2017 So is the field required or not? If it is required, then it should actually say that. You need three checks: If the parameter is present at all. isset() does this. If the date is formally valid. The DateTime::createFromFormat() method does this, but you must check the return value. Don't assume that your jQuery stuff will prevent all input errors. If the date is plausible. In any case, I strongly recommend you use a date picker instead of making the user enter a bunch of digits. It will greatly increase usability und reduce the amount of garbage input. 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.