How can I pass data from one page to another using ajax

0

                  

</head>
<body>

        <form action="llega.php" method="post">
        <input type="text" name="fecha" id="fecha">
        <input type="text" name="direccion" id="direccion">
        <input type="submit" name="enviar">
        <button id="enviar"><a href="llega.php">Enviar</button>
    </form>
 <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script type="text/javascript" src="envia.js"></script>
</body>
</html>

Through ajax we send the variables

 $("#enviar").click(function(){

    fecha =$("#fecha").val();
    direccion =$("#direccion").val();

    console.log(fecha,direccion);

              var datos = new FormData();

            datos.append("fecha", fecha);
            datos.append("direccion",direccion );

            $.ajax({

                url:"llega.php",
                type: "POST",
                data: datos,
                cache: false,
                contentType: false,
                processData: false,
                success: function(respuesta){

    }

        });

    });

The variables arrive to arrive.php but these do not arrive some idea of how to pass them without using a method get

<?php

echo $_POST["direccion"];
echo $_POST["fecha"];

?>
    
asked by Alberto Julio Arce Escolar 11.07.2018 в 08:21
source

3 answers

0

You must remove the form tag:

<input type="text" name="fecha" id="fecha">
<input type="text" name="direccion" id="direccion">
<button id="enviar">Enviar</button>

Ajax would look like this:

$("#enviar").click(function(){

fecha =$("#fecha").val();
direccion =$("#direccion").val();

        $.ajax({

            url:"proceso.php",
            type: "POST",
            data: {
                direccion: direccion,
                fecha: fecha,
            },
            success: function(respuesta){
                window.location.href = 'llega.php';
            }

    });

});

You must create a file called proces.php and in writing this code:

<?php

  session_start();
  $_SESSION['DIRECCION']  = $_POST["direccion"];
  $_SESSION['FECHA'] = $_POST["fecha"];

?>

And in the file llega.php you put:

<?php
  session_start();
  echo $_SESSION['DIRECCION'];
  echo $_SESSION['FECHA'];
?>

I hope it serves you, regards

    
answered by 11.07.2018 / 10:54
source
0

If you are going to use ajax indicating the URL, in the HTML form you do not need to put the action , you can create conflict. And another thing, because you have an input type submit and then a button that calls the page todollega.php . Remove the button and stay with the submit:

<form>
     <input type="text" name="fecha" id="fecha">
     <input type="text" name="direccion" id="direccion">
     <input type="submit" name="enviar" value="Enviar">
</form>
    
answered by 11.07.2018 в 10:16
0

The form is not necessary, because it could cause conflict with the ajax, as well as the input submit and the href are unnecessary, only this would serve you:

        <input type="text" name="fecha" id="fecha">
        <input type="text" name="direccion" id="direccion">
        <button id="enviar">Enviar</button>

Another thing that you have to adjust is the ajax:

$("#enviar").click(function(){

fecha =$("#fecha").val();
direccion =$("#direccion").val();

        $.ajax({

            url:"llega.php",
            type: "POST",
            data: {
                direccion: direccion,
                fecha: fecha,
            },
            success: function(respuesta){

            }

    });

});

And already with that the PHP that you have collects the variables of the ajax.

    
answered by 11.07.2018 в 10:27