Retrieve data fields array() jQuery

Asked

Viewed 504 times

1

I have the following fields in array():

<form enctype="multipart/form-data" id="enviar" method="post">
    <table class="table" style="border-top: 1px solid #FFF !important;">
        <tr>
            <td>L</td>
            <td>Q</td>
            <td>Inicio</td>
            <td>Fim</td>
        </tr>
        <?php 
            for($i=1; $i<=$_GET['lotes']; $i++){ 

                if($i==$_GET['lotes']){
                    $dados['monta_lote'] = $dados['monta_lote'] + $dados['calcula_sobra'];
                }
        ?>  
        <tr>
            <td><span class="widget-thumb-body-stat label label-danger btn-circle" data-counter="counterup" data-value="1"><?php echo $i; ?></span></td>
            <td><span class="widget-thumb-body-stat label label-danger btn-circle" data-counter="counterup" data-value="<?php echo $dados['monta_lote']; ?>"><?php echo $dados['monta_lote']; ?></span></td>
            <td>
                <input type="hidden" id="mensagem" name="mensagem" value="<?php echo $_GET['mensagem']; ?>">
                <input type="hidden" id="lote[]" name="lote[]" value="<?php echo $i; ?>">
                <input type="hidden" id="quantidade[]" name="quantidade[]" value="<?php echo $dados['monta_lote']; ?>">
                <input style="width:145px;" type="date" id="data_inicio[]" name="data_inicio[]" required>
                <input style="width:60px;" type="time" id="hora_inicio[]" name="hora_inicio[]" required>
            </td>
            <td>
                <input style="width:145px;" type="date" id="data_fim[]" name="data_fim[]" required>
                <input style="width:60px;" type="time" id="hora_fim[]" name="hora_fim[]" required>
            </td>
        </tr>
        <?php 
            } 
        ?>
    </table>

    <div class="">
        <center>
            <button type="button" id="agendar" name="agendar" class="btn btn-primary">Agendar Lotes</button>
        </center>
    </div>
</div>

I need that, when clicking send, I search this data that are in these Forms (which can be varios using a foreach()), and send via POST by ajax, I tried as follows:

$(document).ready(function() {
    $('#agendar').click(function() {
        $.ajax({
          type: "POST",
          url: "php/salvarLote.php",
          data: null,
          success: function(data) {
            alert(data)
          },
          error: function(request, status, error) {
            // Aqui você trata um erro que possa vir a ocorrer
            // Exemplo:
            alert("Erro: " + request.responseText);
          }
        });
    });
});

PHP salvarLote.php:

<?php 
  print_r($_POST); // return array()
?>

But when sending, I can’t pass on the contents of these fields. What’s wrong?

  • I posted the complete form, for better analysis, and it is in array() because it can be several fields of these, depending on the amount that the client selected. Ex, selected 4, it will come 4x this form ai. As it is in php.

  • Yes, the data didn’t arrive

1 answer

1


You can get the data from your form using the .serializeArray jquery:

var data = $('#enviar').serializeArray();

and pass in your ajax:

$.ajax({
      type: "POST",
      url: "php/salvarLote.php",
      data: data,
      ...

But the structure that it will separate may not be ideal for you, so I recommend this library: https://github.com/hongymagic/jQuery.serializeObject

Using it, you can do so:

var data = $('#enviar').serializeObject();

And it will be able to assemble the structure according to the hierarchy you specified in HTML.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.