In 1 file a variable takes the datetime, saves it to $_POST automatically with Js, echo prints it, another variable makes the connection and saves it to the phpmyadmin database.
Demonstration:
<?php
//Variable del script:
$timedata = "
<form id='form' method='POST' name='form'>
<input type='text' id='date' name='date' value=''/>
<input type='submit' id='datesubmit'>
</form>
<script type='text/JavaScript'>
const Timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
let d = new Date();
document.getElementById('date').value = d.getDate() +'-'+ d.getMonth() + '-' + d.getFullYear() +' '+ d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() +' '+ ' timezone:' + Timezone;
var form = document.getElementById('form');
form.addEventListener('submit', function(e){
e.preventDefault();
var datos = new FormData(form);
fetch('question.php',{
method: 'POST',
body: datos
})
});
document.getElementById('datesubmit').click();
</script>";
echo $timedata;
//Conexión:
$post = $_POST['date'];
$connection = new PDO('mysql:host=0.0.0.0;dbname=wooddocu_db', "root", "root");
$connection = $connection->query("INSERT INTO users(date) VALUES('$post')");
?>
Result:
Save one with the exact datetime and another empty:
How can you intercept the empty one or save only one?
CORRECTED:
<?php
if ($post = $_POST['date']??null) {//COMPRUEBA QUE EXISTA LA VARIABLE Y SOLO APLICA SI EXISTE
$connection = new PDO('mysql:host=0.0.0.0;dbname=wooddocu_db', "root", "root");
$connection = $connection->query("INSERT INTO users(date) VALUES('$post')");//Esto lo tienes que mejorar porque es un problema de seguridad grave. Revisa la documentación en https://www.php.net/manual/es/pdo.prepare.php
exit;//Con esto cortas la ejecución del script
};
//Variable del script:
$timedata = "
<form id='form' method='POST' name='form'>
<input type='text' id='date' name='date' value=''/>
<input type='submit' id='datesubmit'>
</form>
<script type='text/JavaScript'>
const Timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
let d = new Date();
document.getElementById('date').value = d.getDate() +'-'+ d.getMonth() + '-' + d.getFullYear() +' '+ d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() +' '+ ' timezone:' + Timezone;
var form = document.getElementById('form');
form.addEventListener('submit', function(e){
e.preventDefault();
var datos = new FormData(form);
fetch('question.php',{
method: 'POST',
body: datos
})
});
document.getElementById('datesubmit').click();
</script>";
echo $timedata;
$post = $_POST['date'];
?>
Works. Capture user data, through a form, without pressing the submit button, pass any javascript function to php.
It has a fetch naming the file itself, it sends the POST to itself. It serves in login, start of session, etc. is added with require or include.
you are calling the same page twice, when it is shown and when you send the information
you have to leave the php on another page or validate the data by post, if it comes it is inserted, otherwise it is not done.
First of all, if you have HTML and Javascript code, use them correctly outside of PHP tags.
You have no need to write HTML in PHP when you can do it outside of tags.
First do just the HTML and Javascript and forget about the PHP tags.
Once the form has been left as it corresponds with a correct HTML structure, you can focus on the stored one. If you want to use a single file put the PHP code above the website type:
I'm not going to correct your code but this is how you should organize it. If you pay attention the PHP code is above to avoid duplication of information and therefore not to print HTML code when it is not necessary. (Hence the 'exit')