1

The user is making a coin submission and when they go to do that I want the date to load into a input field that I'm using to track the time the submission was made. That date will be recorded in a mySQL database. The cut off date is set as the coming bimonthly Friday (eg cut off dates are Jun 14 and Jun 28. if the submission is done today, then cut off date is Jun 28. if the submission is done on Jun 30, the cut off date would be Jul 12. I included the entire form so you could get a bigger picture. Feel free to make adjustments. Thanks any help is greatly appreciated.

I'm able to call a javascript function and have the user select the date from a calendar, but that's not what I need. Javascript - get a date from an HTML input but I'm not sure how to concatenate the +14 days for the next cut off date.

CoinSubmission.html

<form action="AdminCoinSub_Code.php" method="POST">
  <h1 id="litheader">Coin Submission</h1>
  <div class="inset">
           <input type="text" list="Store" name="Store" placeholder="Store">
        <datalist id="store">
          <option value="Causeway Bay">
          <option value="Wan Chai">
          <option value="Lai Chi Kok">
          <option value="Tai Po">
        </datalist>    
        <input type="text" list="Position" name="Position" placeholder="Position">
        <datalist id="position">
          <option value="1">
          <option value="2">
          <option value="3">
          <option value="4">
        </datalist>    
        <p>
      <input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
    </p>
    <p>
      <input type="text" name="Contact" id="Contact" placeholder="Contact">
    </p>
    <p>
      <input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
    </p>
    <p>
  <input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
    </p>
    <p>
      <input type="text" name="Coins" id="Coins" placeholder="Coins">
    </p>
    <p>
        <input type="file" type="text" name="location" accept="image/*">
    <div class="btnConfirm">       
    <input class="loginLoginValue" type="hidden" name="" value="" />
    </div>
  </div>
    <div class="btnConfirm">
        <input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
    </div><br><br>
    <div class="wrapper2">
  <nav>
    <ul>
      <li><a href="AdminSubmission.php" class="active">SUBMISSION</a></li>
      <li><a href="OccupancyListAdmin.php">OCCUPANCY</a></li>
      <li><a href="#">ANALYTICS</a></li>
        <li><a href="#">SEARCH</a></li>
    </ul>
  </nav>
    </div>
    </form>

AdminCoinSub_Code.php

<?php {
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "administrator_logins";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO admincoinsubmission (Store, Position, Nickname, Contact, MachineCount, CutOffDate, Coins, location) 
    VALUES ('$_POST[Store]','$_POST[Position]','$_POST[Nickame]','$_POST[Contact]','$_POST[MachineCount]','$_POST[CutOffDate]','$_POST[Coins]','$_POST[location]')");
    $stmt->bindParam(':Store', $Store);
    $stmt->bindParam(':Position', $Position);
    $stmt->bindParam(':Nickname', $Nickname);
    $stmt->bindParam(':Contact', $Contact);
    $stmt->bindParam(':MachineCount', $MachineCount);
    $stmt->bindParam(':CutOffDate', $CutOffDate);
    $stmt->bindParam(':Coins', $Coins);
    $stmt->bindParam(':location', $location);

    $stmt->execute();



    echo "Success";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
}
?>

When the page loads the date row in the coin submission form should display the cutoffdate. 

date = (current date + cutoffdate)
Triptonix
  • 11
  • 5
  • 2
    Your still vulnerable to sql injection. You need to put :Store not $_POST[Store] in your sql. Then get rid of the ':" in the bind. – Jason K Jun 24 '19 at 16:20
  • 1
    You're doing SQL prepared statements wrong! Use ":placeholder" (":Store", ":location", etc) in the SQL, what you're doing is injecting the unvalidated POST data into the query instead – GordonM Jun 24 '19 at 16:44

2 Answers2

1

You can achieve the following task using similar code -

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
       <meta charset="utf-8">
       <title>Date +14 days</title>
    </head>
    <body>
        <input type="date" name="date1" id="date1" value="" />
        <input type="text" name="date2" id="date2" value="" />

        <script type="text/javascript">
            var date1 = document.getElementById("date1");
            date1.addEventListener('change', function(){
                tempDate = new Date(date1.value);
                finalDate = tempDate.setDate(tempDate.getDate() + 14);
                console.log(new Date(finalDate));
            });
       </script>
   </body>
</html>
  • What if, it's suppose to be the bi-weekly Friday of every month so the date that should be loaded right now would be the 28th. The cut off date is set as the coming bimonthly Friday (eg cut off dates are Jun 14 and Jun 28. if the submission is done today, then cut off date is Jun 28. if the submission is done on Jun 30, the cut off date would be Jul 12. – Triptonix Jun 24 '19 at 17:05
0

To achieve expected result, use below option of add time with getTime() method and add 14 days

var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()

Working code sample for reference

var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()
<form action="AdminCoinSub_Code.php" method="POST">
  <h1 id="litheader">Coin Submission</h1>
  <div class="inset">
           <input type="text" list="Store" name="Store" placeholder="Store">
        <datalist id="store">
          <option value="Causeway Bay">
          <option value="Wan Chai">
          <option value="Lai Chi Kok">
          <option value="Tai Po">
        </datalist>    
        <input type="text" list="Position" name="Position" placeholder="Position">
        <datalist id="position">
          <option value="1">
          <option value="2">
          <option value="3">
          <option value="4">
        </datalist>    
        <p>
      <input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
    </p>
    <p>
      <input type="text" name="Contact" id="Contact" placeholder="Contact">
    </p>
    <p>
      <input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
    </p>
    <p>
  <input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
    </p>
    <p>
      <input type="text" name="Coins" id="Coins" placeholder="Coins">
    </p>
    <p>
        <input type="file" type="text" name="location" accept="image/*">
    <div class="btnConfirm">       
    <input class="loginLoginValue" type="hidden" name="" value="" />
    </div>
  </div>
    <div class="btnConfirm">
        <input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
    </div><br><br>
    <div class="wrapper2">
  <nav>
    <ul>
      <li><a href="AdminSubmission.php" class="active">SUBMISSION</a></li>
      <li><a href="OccupancyListAdmin.php">OCCUPANCY</a></li>
      <li><a href="#">ANALYTICS</a></li>
        <li><a href="#">SEARCH</a></li>
    </ul>
  </nav>
    </div>
    </form>

codepen - https://codepen.io/nagasai/pen/VJWmNE?editors=1010

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40
  • What if, it's suppose to be the bi-weekly Friday of every month so the date that should be loaded right now would be the 28th. The cut off date is set as the coming bimonthly Friday (eg cut off dates are Jun 14 and Jun 28. if the submission is done today, then cut off date is Jun 28. if the submission is done on Jun 30, the cut off date would be Jul 12. – Triptonix Jun 24 '19 at 16:56