Link to home
Start Free TrialLog in
Avatar of Scott Johnston
Scott JohnstonFlag for United States of America

asked on

How do i make a PHP form use a specific date format Mask / Validation....

I------have a simple data entry form that I would like to be able to have the Date and Time field be validated so it is on the correct format when being added to the database.  We use this date time field to turn on or off special pricing on products.
In the code below I enter the Starting Date in the correct format "YYYY/MM/DD 00:00:00" on my form and it write to the data base.  I like to have that field "StartingDate" validated on submit.
What is the best solution for this type of validation?

here is my test form:
-------------------------------------------------------------------------------------
<html>
 <head>
 </head>
 <body>
 <form action="insert.php" method="post">
 Item Number: <input type="text" name="ItemNumber">
 Sale Start Date: <input type="datetime" name="StartingDate">
 Sale End Date: <input type-"datetime" name="EndingDate">
 Sale Price: <input type="text" name="SalePrice">
 <input type="submit" name="submit">
 </form>

 


<?php
if (isset($_POST['submit'])){                                              /*only if the submit buttonis presseed*/


 $con = mysql_connect("Datebase location","Logon Name","Passowrd");
 // Check connection
 if (!$con) {
   die ('Could not Connect to MySQL: ' . mysql_error());
 }

mysql_select_db("biomas",$con);
 // escape variables for security
/*$ItemNumber = ($con, $_POST['ItemNumber']);
$StartingDate = ($con, $_POST['StartingDate']);
$EndingDate = ($con, $_POST['EndingDate']);
$SalePrice = ($con, $_POST['SalePrice']);  */

$sql = "INSERT INTO ITProductSale (ItemNumber, StartingDate, EndingDate, SalePrice)
VALUES ('$_POST[ItemNumber]','$_POST[StartingDate]','$_POST[EndingDate]','$_POST[SalePrice]')";

mysql_query ($sql, $con);

/*if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con)); */

echo "1 record added";

mysql_close($con);
}
 ?> 

</body>
 </html>

Open in new window


-------------------------------------------------------------------------
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

According to this page http://www.w3schools.com/html/html5_form_input_types.asp , only Safari and Opera support type="datetime".

I always use <select> dropdowns with the acceptable values as the only options.  I then combine them appropriately in the PHP page that follows.  That way, random values can't be submitted.  There is a some sample code on this page: http://www.dzone.com/snippets/html-select-list-combo-allows
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America 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
Avatar of Scott Johnston

ASKER

I have started from scratch with my coding and I have everything working perfectly but when I retrieve data from MySQL data it will only display the date and not the time.
My database has the date time stored like yyyy-mm-dd hh:mm:ss.

I been fulling around with the DATETIME type on the Input statement but I cannot get the time stamp to display.  I've tried text for type , I've tried Date for type ....not sure what else to do?
I guessing I need to update my value with som special coding?

-----------------------------------------------------------------------------


while ($record = mysql_fetch_array($mydata)){
echo "<form action = updatedeletedata4.php method=post>";
      echo "<tr>";
      echo "<td>" . "<input type=text name=ItemNumber value=" . $record['ItemNumber'] . " </td>";
      echo "<td>" . "<input type=datetime name=StartingDate value=" . $record['StartingDate'] . " </td>";
      echo "<td>" . "<input type=datetime name=EndingDate value=" . $record['EndingDate'] . " </td>";
      echo "<td>" . "<input type=text name=SalePrice value=" . $record['SalePrice'] . " </td>";
      echo "<td>" . "<input type=hidden name=hidden value=" . " </td>";
      echo "<td>" . "<input type=submit name=update value=update" . " </td>";
      echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
      echo "</tr>";
Here is a copy of my new coding....

<html>
<head>
</head>
<body>
      <?php
$con = mysql_connect("domain.com","Account","password");
 // Check connection
 if (!$con) {
   die ('Could not Connect to MySQL: ' . mysql_error());
 }

mysql_select_db("databasename",$con);


if (isset($_POST['update'])){
$UpdateQuery = "Update ITProductSale set ItemNumber='$_POST[Itemnumber]', StartingDate='$_POST[StartingDate]', EndingDate='$_POST[EndingDate]', SalePrice='$_POST[SalePrice]'
where  ItemNumber='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);

};


if (isset($_POST['deleteupdate'])){
$DeleteQuery = "DELETE from ITProductSale where ItemNumber='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);

};



$sql = "Select * from biomas.ITProductSale";
$mydata = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th> Item Number </th>
<th> Starting Date / Time </th>
<th> Ending Date / Time </th>
<th> Sale Price </th>
</tr>";
while ($record = mysql_fetch_array($mydata)){
echo "<form action = updatedeletedata4.php method=post>";
      echo "<tr>";
      echo "<td>" . "<input type=text name=ItemNumber value=" . $record['ItemNumber'] . " </td>";
      echo "<td>" . "<input type=datetime name=StartingDate value=" . $record['StartingDate'] . " </td>";
      echo "<td>" . "<input type=datetime name=EndingDate value=" . $record['EndingDate'] . " </td>";
      echo "<td>" . "<input type=text name=SalePrice value=" . $record['SalePrice'] . " </td>";
      echo "<td>" . "<input type=hidden name=hidden value=" . " </td>";
      echo "<td>" . "<input type=submit name=update value=update" . " </td>";
      echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
      echo "</tr>";
echo "</form>";
}

echo "</table>";
mysql_close($con)

?>

</body>
</html>
SOLUTION
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
Well you guidance is not very help full, it really seems to confuse me more than help me.....Sorry,  Ray as I see it, I will not be able to get the answer from this users group.

I was hoping to learn something, not have to ready a lecture.

I will keep on trying to learn something new but reading tons of documentation about why or why I should not use MYSQL, or information about DATE matrix's, is nothing but confusing, of course it is very informative, but I am a hands on learned.

I learn the best by examples, so I can go through trial and error..

So sorry,

Thank you
Try the examples posted here!
https://www.experts-exchange.com/questions/28506885/How-do-i-make-a-PHP-form-use-a-specific-date-format-Mask-Validation.html?anchorAnswerId=40291171#a40291171

But seriously, if you don't read the articles you're handicapping yourself.  This is stuff every PHP programmer knows.
Ray, I appreciate all your help and your links and I do read them, maybe by me reading the documents and having some examples I will be able to put it all together.  Reading documentation can be very difficult for me to retrieve the information I am looking for because I have no one to review what I have read to make sure I understand and then try to apply what I have just read.

I am currently doing a program that will have a form that will allow me to input data, then I like to use that data to query against the MYSQL data and then display the fields in the database and then make changes to the displayed data, after which would then run a update query to update the date in MYSQL.
I am trying to create a utility for my end user so that they can make direct changes to the MYSQL data and not have to request me to make the change.  (Kind of a simple admin utility for managing a Sales Price on our web site, the sales price is keyed on a item number and then we have a field that is used for start date and time, and end date and time, then sale price.  This is why I was trying to get the date time info.)

After reading about the MYSQL and PHP 5.5...I not sure what I need to do?  but at this time that is another story I will have to address.

I just feel it is easier to learn from example.

I been going through a 40 lesson PHP course (So far I am self taught)and I have learned quit a bit.

Thank you again, I going to keep on trucking, I getting better in understanding simple PHP code.  It can be very hard to learn when you are learning just from tutorials and manuals.
ASKER CERTIFIED SOLUTION
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
I am currently doing a program that will have a form that will allow me to input data, then I like to use that data to query against the MYSQL data and then display the fields in the database and then make changes to the displayed data, after which would then run a update query to update the date in MYSQL.
Of course!  Everyone does that.  It's so common a design pattern that it has a name: A Table Maintenance Script.

Totally agree with Dave: When in doubt, look it up.  I would also add, "make a copy, try a few changes, decide which you like best and store the code and data in a teaching example so you can refer back to it in the future."  That way you don't have reinvent any wheels, and over time you will have built a library of your own preferred solutions.  It's not rocket science, it just takes time and focus.
I agree with both of you regarding reading documentation, it just sometime is hard to get a complete understanding.  I been doing both http://www.w3schools.com/default.asp , and www.php.net.  I get a lot information from both of these sites.  I also been using a site Lecture Snippets, it has some very good information.  I am moving forward and appreciate you insight to my questions.  At this time I still have not gotten the Date format to work correctly.
First quote:
I like to have that field "StartingDate" validated on submit.
Last quote:
I still have not gotten the Date format to work correctly.
I don't know what else to tell you.  If you read the man page for PHP strtotime() and don't "get it" I would expect a follow-on question about best practices in handling date/time values in PHP and MySQL.

There's one other bit of documentation you might want to read: The E-E Grading Guidelines.  You're expected to explain a marked-down grade!