Save multiple checkbox, inputs text together in a database form with ajax

1

I have these two but I do not know how to combine them to save as much as a text image and multiple checkboxes or if they have any other that they can provide me.

$(document).ready(function(){

    $("#formulario").submit(formulario1)
    function formulario1(evento){
        evento.preventDefault()
        var datos = new FormData($("#formulario")[0])
        $("#cargando").html("<img src='img/cargar.gif'>")


        $.ajax({
            url: 'agregar.php',
            type: 'POST',
            data: datos,
            contentType: false,
            processData: false,
            success: function(datos){
                $('#formulario')[0].reset();
                $("#cargando").html(datos)

            }
       })

    }

})

and

$(document).ready(function(){  
      $('#formulario').click(function(){  
           var languages = [];  
           $('.get_value').each(function(){  
                if($(this).is(":checked"))  
                {  
                     languages.push($(this).val());  
                }  
           });  
           languages = languages.toString();  
           $.ajax({  
                url:"agregar.php",  
                method:"POST",  
                data:{languages:languages},  
                success:function(data){  
                     $('#result').html(data);  
                }  
           });  
      });  
 });
    
asked by ivanrangel 05.03.2017 в 23:09
source

1 answer

1

I personally use this code to upload multiple data from the same form and I am doing very well.

AJAX

$(document).ready(function() {
    $(document).on('submit', '#formulario', function() { 

        //obtenemos datos.
        var data = $(this).serialize();  

        $.ajax({  
            type : 'POST',
            url  : 'agregar.php',
            data:  new FormData(this),
            contentType: false,
                  cache: false,
            processData:false,

            success :  function(data) {  
                $('#formulario')[0].reset();
                $("#cargando").html(data);                  
            }
        });

        return false;
    });

});
  

Example to obtain value from our multiple checkbox:

HTML

 <form method="POST" id="formulario" enctype="multipart/form-data">

     <input type="checkbox" name="buckets[]" value="valor 1">
     <input type="checkbox" name="buckets[]" value="valor 2">
     <input type="checkbox" name="buckets[]" value="valor 3">

     <!-- imagen, otros datos, etc. -->

     <button type="submit">Guardar</button>
</form>

<div id="cargando"><!-- Respuesta AJAX --></div>

add.php

<?php
//var_dump($_POST);

if(!empty($_POST['buckets'])) {
    foreach($_POST['buckets'] as $bucket) {
        echo $bucket; //Mostramos el resultaso seleccionado.                
    }
}

//Continuas con tu código para guardar tus datos.
?>
    
answered by 06.03.2017 / 00:31
source