Passing PHP Javascript PHP Parameters

Asked

Viewed 246 times

-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);
?>

2 answers

1

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

In this code snippet, you are taking the element by the ID, but in your HTML the field Ids are different, and as you are using jQuery, to take the value of a field can be used the method. val() http://api.jquery.com/val/ which is much easier

Stay like this

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

The passing of the parameters is correct, but you did not get them correctly. I hope I’ve helped

-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’ve helped :)

  • Why not take advantage and explain what you did? What was the problem in the question code and how it solved?

  • 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.

  • 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.