Recover fix in javascript and send it to php


I have a problem I can not solve, I have a form which contains a section inputs checkbox that are generated dynamically, the name of this input keep it as mail_2 [] my problem is how can Pick up that array in js to send it to php where the array will be treated?

My question would be then, how to send the fix from js to php? I intend it in the following way, but when I see it, it seems that nothing is sent:

mail_2 = document.mailing.elements['mail_2[]'];

My input:

<div class="form-group">
              <label>Promociones Disponibles:</label>
              <div class="checkbox checkbox-primary">
                           while($f=mysql_fetch_array($re)) { ?>
                               <input name="mail_2[]"  id="mail_2" class="styled" type="checkbox" value="<?php echo $f[llave] ?>">
                                <?php echo $f[titulo] ?>    
                          <?php }

This is my complete function:

 function mailing_envio(){

    var selectedValues = [];    
    $("#ms :selected").each(function(){
    //return false;

    titulo_mail = document.mailing.titulo_mail.value;
    mail_2 = document.mailing.elements['mail_2[]'];

    ajax = objetoAjax();
    //alert("correo para usuarios");"POST", "class/mailing_enviar.php", true);
    }else if(selectedValues=='prospectos'){
    alert("correo para prospectos"+mail_2);
    //"POST", "class/actualizar.php", true);
     alert("correo para todos");   

    ajax.onreadystatechange=function() {
        if (ajax.readyState==2) {
            alert("Datos enviados)
asked by Agustin Acosta 14.03.2016 в 22:35

1 answer


Since all the elements are checkboxes that have the name mail_2[] , you can select them based on the name and then concatenate the values of the boxes that are marked.

One possible option for this. Instead of doing this:

mail_2 = document.mailing.elements['mail_2[]'];

Select the boxes that have the name mail_2[] and are marked, and concatenates their values. The code would look something like this:

// array que contendrá los valores seleccionados
var valores = new Array();
// array con los checkboxes que tienen nombre mail_2[] y que están marcados
var inputs = document.querySelectorAll("[name='mail_2[]']:checked");
// atravesamos el array de inputs
for (var x = 0; x < inputs.length; x++) {
    // insertando su valores en el array de valores
// asignamos a mail_2 la concatenación de los valores
mail_2 = valores.join(",");

In this way, the variable mail_2 will contain a text string with the selected values separated by a comma, and you could keep the rest of the code in a similar way as it is now.

answered by 14.03.2016 / 22:57