Looking for a solution to an issue that I am having. My PHP file is able to store a date and time from a user's input. However, once the date is stored, it is no longer accessible to be used in my Javascript function.
I am just lost on how data is processed step by step. How would I extract stored data in my database to use in my Javascript function?
UPDATE: ADDED CODE
PHP: Simply stores input, and shows it was stored
<?php
// ONLY Process Form if $_POST is NOT Empty
if ( ! empty( $_POST ) ) {
// Connect to MySQL
$mysqli = new mysqli( 'hn', 'un', 'pw', 'db' );
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$sql = "INSERT INTO table ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['myDate'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
}
?>
HTML: Just a form
<html>
<head>
<meta charset="utf-8">
<title>User Form</title>
</head>
<script type="text/javascript" src="/displaytimer.js"></script>
<body>
<!-- HTML Form -->
<form class="" action="/index.php" method="post">
<input type="datetime-local" name="myDate" id="myDate"><br /><br />
<button id="submit" onclick="clickButton()" value="submit">Submit</button><br /><br />
<div id="showDate"></div>
</form>
</body>
</html>
JAVASCRIPT: Generates a timer based on users inputed date. Also accessible on iPhone (found help here on SO for that). I am sure that there is a simpler way to do this, but I am mainly looking for how to still use the date inputed within var n.
// get the iso time string formatted for usage in an input['type="datetime-local"']
var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0,-1);
var localISOTimeWithoutSeconds = localISOTime.slice(0,16);
// select the "datetime-local" input to set the default value on
var dtlInput = document.querySelector('input[type="datetime-local"]');
// set it and forget it ;)
dtlInput.value = localISOTime.slice(0,16);
function clickButton() {
var x = setInterval(function() {
// Get Date Selected & Convert to ISO
var n = document.getElementById("myDate").value;
var d = new Date(n);
var dISO = d.toISOString();
// Get Today's Date
var today = new Date();
var tS = today.toDateString();
var tISO = today.toISOString();
// Calculate Time Left
var dParse = Date.parse(dISO);
var tParse = Date.parse(tISO);
var distance = dParse - tParse;
// days
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
if (days > 0) {
days = Math.floor(distance / (1000 * 60 * 60 * 24));
} else {
days = 0;
}
// hours
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
if (hours > 0) {
hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
} else {
hours = 0;
}
// minutes
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
if (minutes > 0) {
minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
} else {
minutes = 0;
}
// seconds
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (seconds > 0) {
seconds = Math.floor((distance % (1000 * 60)) / 1000);
} else {
seconds = 0;
}
// Print Distance or Expired Message
if (days > 0 || hours > 0 || days > 0 || seconds > 0) {
document.getElementById("showDate").innerHTML = days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
} else {
document.getElementById("showDate").innerHTML = "The time has expired";
}
}, 1000);
}