send data through ajax of a select

1

I have the following lines

$(document).on('change', '#select_encuestas', function(event) {
        //alert( this.value );
        //alert("sssd");
             var x = document.getElementById("select_encuestas").value;
             //alert("id.."+x);
             console.log();
             if(x!=0){
                    $("#agregarSeccion").show();    
             }else{
                $("#agregarSeccion").hide();    
             }
            // definirEncuesta(x);
             parametros="id="+x;
                $.ajax({
                    url:'secciones_ajax.php',
                    data: parametros,
                     beforeSend: function(objeto){
                    $("#loader").html("<center><img src='loader.gif'></center>");
                    },
                    success:function(data){
                    $(".outer_div").html(data).fadeIn('slow');
                    $("#loader").html("");
                //  load(1);
                  }
            });
          event.preventDefault()

I need to send the id to the pagina_ajax page but apparently it is not sent in id ... I thought it was the Event but I think it does not work like that

    
asked by jDanielSotoC 12.03.2018 в 23:28
source

2 answers

1

This line:

 parametros="id="+x;

You could replace it with:

var parametros = $('#formulario').serialize();

The above to deal with the serialize method that reads all the inputs of a form and takes them ready within the var parameters

To be able to read all the inputs of a form I put in the form tag an id in this case form

<form id="formulario" method="POST">

At the end of your AJAX method, the rest is the same, so at the end you do not have to declare one by one of the values, but they all go together

Greetings and tell me if it served

    
answered by 12.03.2018 в 23:40
1

Since you are using jquery, it is better to use it for the entire script. If you want to get the value of a select do the following:

$(document).on('change', '#select_encuestas', function(event) {
    //alert( this.value );
    //alert("sssd");
         var x = $("#select_encuestas option:selected").val();
         //alert("id.."+x);
         console.log();
         if(x!=0){
                $("#agregarSeccion").show();    
         }else{
            $("#agregarSeccion").hide();    
         }
        // definirEncuesta(x);
            $.ajax({
                url:'secciones_ajax.php',
                data: {id:x},
                 beforeSend: function(objeto){
                $("#loader").html("<center><img src='loader.gif'></center>");
                },
                success:function(data){
                $(".outer_div").html(data).fadeIn('slow');
                $("#loader").html("");
            //  load(1);
              }
        });
      event.preventDefault()});
    
answered by 13.03.2018 в 00:24