I need to pass a parameter via JSON from a PHP page ➝ Javascript ➝ PHP, I'm using the Ajax method of Jquery but I'm not getting it, it follows code snippet.
✓ Index.php
<html>
<head></head>
<body>
<input id="dateS" name="dateS" type="text" class="form-control" maxlength="10" placeholder="mm/dd/yyyy"></div>
<label class="col-form-label">entre</label>
<div class="col-3">
<input id="dateE" name="dateE" type="text" class="form-control" maxlength="10" placeholder="mm/dd/yyyy"></div>
<input id="pesquisa" type="submit" class="btn btn-primary" value="Pesquisar"/>
</body>
</html>
AAjax
function mostrarValor() {
alert(document.getElementById("data1").value);
}
var datainicio = document.getElementById('data1');
var datafim = document.getElementById('data2');
document.getElementById("pesquisa").onclick = function(e) {
mostrarValor();
e.preventDefault();
}
$.ajax({
method: 'post',
url: '/dash/charts/data_acesso.php',
data: { 'DataInicio': datainicio, 'DataFim': datafim, }
});
$.post('/dash/charts/data_acesso.php', data, function(data) {
console.log(data);
});
✓ File that receives Json for another generation
<?php
//Setting Json
header('content-type: application/json');
$datainicio = $_POST['datainicio'];
$datafim = $_POST['datafim'];
//Query por superintendência
$sql = "SELECT SUBSTRING(u.department FROM 1 FOR 12) AS 'department' , COUNT(l.action) AS 'qtdacesso'
FROM mdl_user u
INNER jOIN mdl_logstore_standard_log l ON u.id = l.userid
WHERE action LIKE 'loggedin' AND u.department NOT LIKE ''
GROUP BY l.action,u.department";
//Execute
$result=$DB->get_records_sql($sql);
//Array
$dados = array();
foreach($result as $row){
$dados[] = $row;
}
print json_encode($dados, JSON_PRETTY_PRINT);
?>