Passing javascript data to php

0

I'm trying to pass some data from a form by passing it through ajax but for some reason the page that sends the data does not arrive at all. This is my javascript code:

        </script>
            <!-- Apertura Script -->
            <script>
                $(document).ready(function(){
                $('#envio').click(function(){
                 var usuario = document.getElementById("nombre").value;
                 var telefono = document.getElementById("telefono").value;
                 var comentario = document.getElementById("comentario").value;
                  document.getElementById("usuario").innerHTML = usuario;
                    $("#formularioUsuario").hide(0);
                  $("#mensajeDespedida").show(800);
                  var url = "form.php";                                      
                  var parametros = {
                    "nombre" : usuario,
                    "telefono" : telefono,
                    "comentario" : comentario
    };
                  $.ajax({                        
                     type: "POST",                 
                     url: url,                    
                     data: parametros,
                     success: function(data, textStatus, jqXHR)             
                    {

                        location.href ="http://localhost/a/form.php";          
                    },
                    error: function(jqXHR, textStatus, errorThrown) { 
                    alert('Ha ocurrido un error'); 
                }         
                   });
                });
                });
                        </script>
                        <!-- Cierre script -->

And this is my php code:

<?php
if(isset($_POST['telefono'])){
echo "Llegó";
$nombre=$_POST['nombre'];
$telefono = $_POST['telefono'];
$comentario = $_POST['comentario'];

$resultado = $nombre=$_POST['nombre'] .  $telefono = $_POST['telefono'] .  $comentario = $_POST['comentario'];
$json = json_encode($resultado);
echo $json;
}

else {
    echo "No ha llegado";
}
?>

I also add HTML, this triggers a modal:

 <div id="formularioUsuario">
        <form id="formUser" name="formUser" method="post"  class="col-md-4 control-label">
        <div class="form-group">
            <label for="nombre"> Nombre:
                <input type="text" id="nombre" name="nombre" class="form-control" autofocus>
            </label>
        </div>
        <div class="form-group">
            <label for="telefono"> Telefono:
                <input type="tel" id="telefono" name="telefono" class="form-control">
            </label>
        </div>
            <div class="form-group" id="test">
            <label for="telefono"> Comentario:</label><br>
            <textarea  id="comentario" name="comentario" class="form-control" rows="4" cols="5" maxlength="100" placeholder="Agrega información adicional de lo que quieres que hablemos."></textarea> 
            </br>
            <div class="modal-body">

        </div>      
                <input type="button" id="envio" value="Enviar">        
        </form>
        </div>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

</div>

The ajax does not throw me any errors but when loading the page "form.php" the echo fired is "It has not arrived".

I do not see the error. What could it be?

    
asked by Kryphon 13.08.2018 в 19:44
source

1 answer

0

I already understood your problem what happens is that when the ajax returns the answer you redirect it, comment the line of

location.href ="http://localhost/a/form.php";

and place an alert in the function sucsess with the variable data asi

alert(data);  

So you'll see the answer that your php returns

    
answered by 13.08.2018 / 20:15
source