-3
I need to pass a parameters via JSON of a PHP page Javascript PHP, I’m using the Ajax method of Jquery but I’m not getting, follow 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>
Ajax
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);
?>
Why not take advantage and explain what you did? What was the problem in the question code and how it solved?
– Woss
Like "hoping to help" when all you did was paste code? Consider giving an explanation to the questioner, since questions asked on the site are not intended to solve only the question author’s problem, but anyone who is looking for and falls on this page.
– Wallace Maxters
The Ajax code executes as soon as you click on the search element (I only did it in a simpler way); in php you use header(json) but when you send the information you use json_encode, that is, the text must be header(text/Plain) for the conversion;
– Guilherme Santos