JS parameter for PHP file

Asked

Viewed 520 times

3

Hello.. I am having a problem in which I am encountering difficulties, perhaps by my little 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 a JSON is returned... Okay... my question is this::

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

2 answers

3

Why not make a $_GET?

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

In the file Events.php:

$parametro = $_GET['param']; // 123
  • Of course, that’s right. I was getting _REQUEST on the other page, so I was making a mistake. Thank you.

  • Glad you could help

0

I recommend using jQuery and AJAX for this procedure, because there may be some error in the procedure and you have a much wider range of resources to handle the returns of the HTTP protocol. You add jQuery to your project and create a script more or less 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 use this function better I recommend reading the documentation at this link.

Browser other questions tagged

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