Pass Javascript parameter to PHP

2

I want to pass 2 (two) parameters from my javascript page to a PHP page. I already searched the net, and I only find this logic when PHP is on the same page as javascript.

Does anyone know if this is possible? and how to do it?

    
asked by anonymous 02.07.2015 / 19:26

3 answers

2

Can not , unless you are trying to HTTP request (ajax, forms, etc.).

This is because JavaScript runs client side (browser) while PHP runs server side (server) and the only path from JavaScript (client) to PHP (server) is through HTTP request %.

Example - Ajax:

ajax.html

$.ajax({
  type: "POST",
  url: "algum.php",
  data: { meuParametro1: "valor 1", meuParametro2: "valor 2"},
  complete: function(data){
            // (...)
        }
});

Example - Form:

formulario.html

<form action="algum.php" method="post">
    <<input type="text" name="meuParametro1" value="valor 1">
    <<input type="text" name="meuParametro2" value="valor 2">

    <<input type="button" name="btEnviar" value="Enviar">
</form>

algum.php

$parametro1 = $_POST['meuParametro1'];
$parametro2 = $_POST['meuParametro2'];
    
02.07.2015 / 19:32
1

You can add as many variables as you want. Exp:

data: { parametro: "valor", prametro2: "valor", parametro3: "valor"}

Or go from PHP like this:

var valor1= '<?php echo "valor"; ?>'
var valor2 = '<?php echo "valor"; ?>'

data: { parametro: valor1, parametro2: valor2}
    
02.07.2015 / 19:49
0

Use ajax:

$.ajax({
      type: "POST",
      data: {variavel: 'valor'} ,
      url: "arquivo.php",
      success: function(resposta){
          alert(resposta);
      }  
    }); 

This way you give a PHP POST of PHP.

    
02.07.2015 / 19:33