Passing of PHP Parameters ➝ Javascript ➝PHP [closed]

-3

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);
?>
    
asked by anonymous 16.11.2017 / 12:44

2 answers

1
var datainicio = document.getElementById('data1');
var datafim = document.getElementById('data2');

In this section of the code, you are getting the element by the ID, but in your HTML the field IDs are different, and since you are using jQuery, to get the value of a field you can use the .val () link that is much easier

Stay the same

var dateS = $('#dateS').val();

Parameter passing is correct, however you did not pick it up correctly. I hope I have helped

    
16.11.2017 / 13:49
-4

Hello,

Index.HTML

<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

$("#pesquisa").click(function(){

           var datainicio = $("#dateS").val();
		   var datafim =  $("#dateE").val();

            jQuery.ajax({
				type:'POST',
				url:'/dash/charts/data_acesso.php',
				data: {'DataInicio' : datainicio ,'DataFim' : datafim ,},
				success: function(data)
				{
				     console.log(data);
				}
			});




}

PHP file that receives Json

<?php
//Setting Json
header("Content-Type: text/plain");
$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_r(json_encode($dados));
?>

I hope I have helped:)

    
16.11.2017 / 12:56