Jump to content

How do I ensure that a time can only be selected at least 24 hours after submission through a form?


Recommended Posts

The question is pretty difficult to ask, but I'll try and be as clear as possible. Pretty much I have created a form where a user submits a request and in my SQL table it autogenerates the time for when the user clicks submit. The code for the SQL part is:

request_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

In my form I have a html calender input, in the format YY-MM-DD and I have a separate time input in the format HH:MM. I am pretty much trying to ensure that a date earlier than the current date cannot be selected, but I am having a bit of a hard time.

Here is my php/html code for reference:

<html>
<head>
<title>ShipOnline Request System</title>
</head>
<body>
<h2>ShipOnline System Request Page</h2>
<fieldset>
<p>Item Information:</p>
<fieldset>
<form method="post">
<p><label for="description">Description: <input type="text" name="description" id="description"></label></p>
<p><label for="weight">Weight: </label>
<select name="weight" id="weight">
    <option value= "">Select Weight</option>
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
</select></p>
<p>Weight is charged at a flat rate of $10 for 0-2Kg and an additional $2 per kg, with a max of 20kg.</p>
</fieldset>
<p>Pick-up Information:</p>
<fieldset>
<p><label>Address: <input type="text" name="address" id="address"></label></p>
<p><label>Suburb: <input type="text" name="suburb" id="suburb"></label></p>
<p><label for="Date">Preferred Date: </label>
<input type="date" name="date"></p>
<p><label for="time">Preferred Time: </label>
<input type="time" name="time" min="07:30" max="20:30"></p>
<p>Time must be between 7:30am and 8:30pm</p>
</fieldset>
<p>Delivery Information</p>
<fieldset>
<p><label>Receiver Name: <input type="text" name="name" id="name"></label></p>
<p><label>Address: <input type="text" name="address1" id="address"></label></p>
<p><label>Suburb: <input type="text" name="suburb1" id="suburb"></label></p>
<p><label for="state">State: </label>
<select name="state" id="state">
    <option value="">Select State</option>
    <option value="vic">VIC</option>
    <option value="nsw">NSW</option>
    <option value="tas">TAS</option>
    <option value="sa">SA</option>
    <option value="act">ACT</option>
    <option value="wa">WA</option>
    <option value="nt">NT</option>
</select></p>
</fieldset>
<p><input type= "submit" value="Request" ></p>
    
</fieldset>
</form>
<?php
$custnum= $_GET['custno'];
if(isset($_POST['description']) && isset($_POST['weight']) && isset($_POST['address']) && isset($_POST['suburb']) && isset($_POST['date']) && isset($_POST['time']) && isset($_POST['name']) && isset($_POST['address1']) && isset($_POST['suburb1']) && isset($_POST['state'])) {
    if (empty($_POST['description']) ||
    empty($_POST['weight']) ||
    empty($_POST['address']) ||
    empty($_POST['suburb']) ||
    empty($_POST['date']) ||
    empty($_POST['time']) ||
    empty($_POST['name']) ||
    empty($_POST['address1']) ||
    empty($_POST['suburb1']) ||
    empty($_POST['state'])) {
    
    die('Please fill all required fields!');
    }

    $DBConnect = @mysqli_connect("", "", "", "")
            Or die ("<p> Unable to connect to the database server.</p>". "<p>Error code". mysqli_connect_errno().":".mysqli_connect_error()). ",/p>"; 
    $description= mysqli_real_escape_string($DBConnect, $_POST['description']);
    $weight= mysqli_real_escape_string($DBConnect, $_POST['weight']);
    $address= mysqli_real_escape_string($DBConnect, $_POST['address']);
    $suburb= mysqli_real_escape_string($DBConnect, $_POST['suburb']);
    $date = mysqli_real_escape_string($DBConnect, $_POST['date']);
    $time= mysqli_real_escape_string($DBConnect, $_POST['time']);
    $name= mysqli_real_escape_string($DBConnect, $_POST['name']);
    $address_one= mysqli_real_escape_string($DBConnect, $_POST['address1']);
    $suburb_one=mysqli_real_escape_string($DBConnect, $_POST['suburb1']);
    $state= mysqli_real_escape_string($DBConnect, $_POST['state']);
    $price= max($weight - 2, 0)*2 + 10;
    $requestDate= mysqli_query($DBConnect, 'SELECT request_date FROM request');
    $totaltime= $date."".$time.":00";
    if ($requestDate>$totaltime) {
        die("Pick up Time must be at least 24 hours after request is sent!");
    }
    $SQLString= "INSERT INTO request (customer_number, item_description, weight, pickup_add, suburb, pickup_date, pickup_time, name, address, receiver_suburb, state) VALUES ('$custnum', '$description', '$weight', '$address', '$suburb', '$date','$time' ,'$name', '$address_one', '$suburb_one', '$state')";
    if(mysqli_query($DBConnect, $SQLString)){
        $reqno= $DBConnect -> insert_id;
        echo "Thank you! Your request number is $reqno. The cost is $$price. We will pick-up the item at $time on $date.";
    }else{
            echo "ERROR";
}
        mysqli_close($DBConnect);
}

?>
</body>
</html>

Any help would be appreciated thank you!

Edited by trenta99

I would simply receive the incoming form's data and build a php datetime value from the user's entries.  Then I would compare it to a value of today + 24 hours and ensure that the user gave you a datetime > than that.   Read up on datetime values/methods in the manual.

22 minutes ago, trenta99 said:

I am pretty much trying to ensure that a date earlier than the current date cannot be selected

 

If you want what your topic title stated it would be

SELECT ..... FROM request
WHERE request_date + INTERVAL 24 HOUR <= NOW()

 

15 minutes ago, ginerjm said:

I would simply receive the incoming form's data and build a php datetime value from the user's entries.  Then I would compare it to a value of today + 24 hours and ensure that the user gave you a datetime > than that.   Read up on datetime values/methods in the manual.

Hey ginerjm, can you elaborate a bit more sorry! I am still really new to php!

Your 2 inputs are coming to your script in the $_POST array.  Say they are called 'input_date' and 'input_time', named in their <input> tags using the 'name' attribute.  (Read up on input tags).  You can use php to create a date type variable using 'strototime' function (read up on it in the php manual)  Now you just have to create another date value from the actual date of the submission (as you said) so that is simply $today = date().  Now compare the 2 values.

I could write you some code but a) I don't feel like it today and b) you need to learn about this yourself and reading up on these activities will only serve to help you get up to speed.  Then you won't be "still really new to php".

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.