Send Parameters by URL

1

Code: I have 2 variables

<script type="text/javascript" src="code.jquery.com/jquery-3.2.1.min.js"></script> 
<script>
 var var1 = $("#var1").val(); var var2 = $("#var2").val(); 
$.ajax({ 
url:'recibo.php', 
data:{var1:var1,var2:var2}, 
type:'POST', 
datatype:'json' 
})
</script> 

My intention is to send them by the url HIDDEN OR ENCRYPTED or if there is another way to send them either by JQUERY OR AJAX

My intention is to send the values to another page with the following code .. Page Example.php

 <?php
    $var1=1;
    $var2=2;
    echo "<div class='inner arriba' id='for'>
     <a href='#'><input type='text' name='var1' id='var1' value='$var1'>      <input type='text' name='var2' id='var2' value='$var2'></a>
      </div>";
     ?>
     <html>

     <head>

     <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

     <script>
         var var1 = $("#var1").val();
         var var2 = $("#var2").val();
         $.ajax({
          url:'recibo.php',
          data:{var1:var1,var2:var2},
         type:'POST',
         datatype:'json'
     })

         var jqxhr = $.ajax( "recibo.php" )
         .done(function() {
          alert("Succes");
     })
         .fail(function() {
          alert( "error" );
      })
         .always(function() {
          alert( "complete" );
      });

        jqxhr.always(function() {
       alert( "second complete" );
    });
    </script>
    </head>
      <body>
     </body>
     </html>

Page emjemplo2.php

Here I want to receive the sent values, I am investigating how to receive it with json since the shipping method I use is datatype: 'json'

    <?php 
     echo $resultado = $_POST['var1'] + $_POST['var2']; 
      echo $ca=$_POST['var1'];
      echo $ca2=$_POST['var2'];
    ?>
    
asked by David 27.07.2017 в 20:15
source

2 answers

1

The syntax for using Ajax in Jquery is as follows:

    $.ajax({
    url:'direcciónURL',
    data:{var1:var1,var1:var1},
    type:'GET or POST',
    datatype:'json'

 })
 .done(function(data, textStatus, jqXHR) {

      console.log( "La solicitud se ha completado correctamente." );

   })
 .fail(function(jqXHR, textStatus, errorThrown) {
         console.log( "La solicitud a fallado: " +  textStatus)
  });
});

Your PHP should be something like this:

<?php 

$var1 = $_POST["var1"];
$var2 = $_POST["var2"];

echo "Valor 1: ".$var1." Valor 2: ".$var2

?>

link

    
answered by 27.07.2017 / 20:28
source
0

Another way you can do it with JQuery is to use the $.post() function, which is a bit easier in its syntax.

$.post("verifi.php",
    {
        variable1: var1,
        variable2: var2
    },
    function(ResultadoPost){
        //ResultadoPost trae cualquier cosa que tu página verifi.php haya servido
});

You can see the $.post() information on this link: link

Some important clarifications:

  • JQuery (JavaScript library) can get the information of any HTML element that is assigned a value , but can not do it of PHP , I hope you have this very clear.
  • So, in order to use this function, the value of $var1 or $var2 would have to be assigned to some element of your HTML. As for example :

    <input id="variable1" value=<?php echo $var1; ?> type="text">

    Or any other HTML element that has the value property. A Once there you get the value with a JQuery selector

    var var1 = $("#variable1").value();

    That as it is observed in my answer is the value that you send to your file verifi.php .

  • The callback , which is the function that is after the variables which you send, is completely optional.

  • And again I remember, this process is given from an HTML page already served (that is, something that is already in the user's browser) to a PHP file on the server. (I only make the explanation for the almost contradiction that you have in your question)
answered by 27.07.2017 в 20:51