A .serialize () does not take the form data

1

I have a problem with jQuery.

The problem is that I have an AJAX request to send me two data to another .php page that asks me a question.

<script type="text/javascript">
    $(document).ready(function(){
        var data=$("form").serialize();
        function obtener_modificacion(){
            $.ajax({                        
                type:"POST",                 
                url:"prueba2.php",
                success: function(data)             
                {
                    $('#consulta').html(data);               
                }
            });
        }
        $(document).on("click", "#buscar", function(){
            $.ajax({
                type:"POST",
                url:"prueba2.php",
                data:data,
                success: function(data){
                    obtener_modificacion();
                    alert(data);
                }
            })
        });
    });
</script>

And this is the form:

<form name="form" id="form" method="POST">
    <select name="tipodoc" id="tipodoc">
        <option value="0">Tipo de documento</option>
        <option value="T.I.">Tarjeta de identidad</option>
        <option value="C.C.">C&eacute;dula de ciudadan&iacute;a</option>
        <option value="C.E.">C&eacute;dula de extranjer&iacute;a</option>
    </select>
    <input type="number" name="documento" placeholder="N&uacute;mero de documento" id="documento" required>
    <input type="submit" name="buscar" value="Consultar" id="buscar"></input>
</form>
<section id="consulta"></section>

This is the .php page that receives the data (prueba2.php):

<?php
require('conexion.php');
if (isset($_POST['documento']) && isset($_POST['tipodoc'])) {
    $doc=$_POST['documento'];
    $tipodoc=$_POST['tipodoc'];
    echo $tipodoc . " " . $doc . "<br>";
}
?>

When wanting to send the data, the variables are generated, but without values; Also, it does not bring me the echo of the .php

    
asked by Santiago Correa Aguirre Sanmar 28.11.2018 в 23:28
source

2 answers

1

Your code is wrong, here I find the origin of the double request Ajax of your other question that is bringing problems.

It is not correct what you are doing, calling an Ajax request from within another Ajax request to the same file. This would not make any sense. I'll show you how to make a single request:

<script type="text/javascript">
    $(document).ready(function(){
        $( "#buscar" ).click(function( event ) {
            /*Con esto evitamos que se recargue la página*/
            event.preventDefault();
            /*Es más específico usar el id*/
            var data=$("#form").serialize();
            $.ajax({                        
                method:"POST",                 
                url:"prueba2.php",
                data: data,
                success: function(data)             
                {
                    $('#consulta').html(data);               
                }
            });
            })
    });
</script>

Do the same in the code of your other question, simplify the code and avoid errors because Ajax requests are asynchronous.

    
answered by 29.11.2018 в 02:45
0

You have to serialize the data of the form just before sending them, in your code you do it only once when you load the page ( $(document).ready )

    $(document).on("click", "#buscar", function(){

        var data = $("form").serialize(); // Agrega esta línea

        $.ajax({
            type:"POST",
            url:"prueba2.php",
            data:data,
            success: function(data){
                obtener_modificacion();
                alert(data);
            }
        })
    });
    
answered by 28.11.2018 в 23:40