JS parameter for PHP file

3

Hello .. I have a problem in which I am encountering difficulties, perhaps due to my lack of knowledge in JS.

I have a JavaScript function that calls a PHP file:

<script type='text/javascript'>
...

 events: "events.php"
</script>

When this PHP file is called (events.php) everything that needs to happen happens. A search is performed on this file and returned a JSON ... Ok ... my question is this:

I need to call the file 'events.php' to pass a variable as a parameter, to get its value inside the called file. How can I do this? How to pass a php variable as a parameter in the JS function that calls a PHP file?

    
asked by anonymous 23.05.2016 / 05:46

2 answers

3

Why not make a $_GET ?

<script type='text/javascript'>
...
 var parametro = 123;
 events: "events.php?param=" +parametro;
</script>

In the events.php file:

$parametro = $_GET['param']; // 123
    
23.05.2016 / 09:30
0

I recommend that you use jQuery and AJAX for this procedure because some procedural error may occur and you have a much wider range of capabilities to handle the HTTP protocol returns. You add jQuery to your project and create a script something like this:

$.ajax({
  method: "GET",
  url: "events.php",
  data: { dado1: "blablabla", dado2: "blablabla" },
  success: function (msg){
    //aqui ficará o retorno da página
    alert (msg);
  },
  error: function (){
    //Faz alguma coisa caso não obtenha sucesso
  }
});

To better use this function, I recommend reading the documentation .

    
23.05.2016 / 13:25