Pass values to PHP file with JSON

Asked

Viewed 861 times

2

I have the code below that runs on any PC and asks for information for a PHP file on a remote server.

How do I pass a parameter to this PHP using my JSON code?

So I can send a data and have a processed return.

I know it sounds simple, but I could only run in this scenario (HTML anywhere) and PHP (remote server) with this code. All other methods need to have all files on the server.

HTML + JS file (Runs on any computer running)

<script type="text/javascript"> 
var urlTeste = 'http://www.meuservidor.com/servidor.php?jsoncallback=?';
$(document).ready(function() {
//Mensagem enquanto não carrega a pagina
$('#resultado').html('Carregando...');

$.getJSON(urlTeste,null, function(data){
$('#resultado').html(data);   
});
}); 
</script>

PHP file (on www.meuservidor.com/server.php server)

<?php
$var = date("d/m/Y H:i:s "); 
echo $_GET["jsoncallback"] . '(' . json_encode($var) . ');';    
?>

Thank you

1 answer

3

Over there where you’re passing null, pass an object:

$.getJSON(urlTeste, {chave: "valor", outro: "outro valor"}, function(data){
   $('#resultado').html(data);   
});

And in PHP:

<?php
$chave = $_GET['chave'];
$outro = $_GET['outro']; 
  • 1

    And thank you for following my recommendation in the other question :)

  • In short, the call basically has to look like this: $.getJSON("url.php", {key: "value", other: "other value"}, Function(date)? $('#result').html(date); }); the code posted by @bfavaretto is very correct, I always use the call in this way in my projects. Abs

  • It really worked properly. Show! Thank you so much for your help.

Browser other questions tagged

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