0

I have a huge form to submit (more than 1000 inputs in array - this is a problem for php +5.3 see max_input_vars limits) in php.

I don´t need to submit everything, only the group of modified inputs where the reference is a select input. I'm trying to use jQuery filter and serializeArray() to solve it, but it's not working. I have to use ajax to do it.

My form in a php loop with data from a DB

<form method="post" action="" id="resultado_trabalhos" name="resultado_trabalhos">

    <?php do { ?>
<select name="status_trabalho[]" id="status_trabalho<?php echo $row_listaTrabalhos['id_usuario']; ?>">
<option value="3" <?php if (!(strcmp(3, $row_listaTrabalhos['status_trabalho']))) {echo "selected=\"selected\"";} ?>>em avaliação</option>
<option value="4" <?php if (!(strcmp(4, $row_listaTrabalhos['status_trabalho']))) {echo "selected=\"selected\"";} ?>>oral</option>
<option value="5" <?php if (!(strcmp(5, $row_listaTrabalhos['status_trabalho']))) {echo "selected=\"selected\"";} ?>>poster</option>
<option value="6" <?php if (!(strcmp(6, $row_listaTrabalhos['status_trabalho']))) {echo "selected=\"selected\"";} ?>>não selecionado</option>
</select>

<textarea name="motivoDevolucao[]" id="motivoDevolucao<?php echo $row_listaTrabalhos['id_trabalho']; ?>" cols="100" rows="2" wrap="physical" style="display:none;">&nbsp;</textarea>

  <input type="hidden" id="id_autor<?php echo $row_listaTrabalhos['id_usuario']; ?>" name="id_autor[]" value="<?php echo $row_listaTrabalhos['id_usuario']; ?>" />
   <input type="hidden" id="token_trabalho<?php echo $row_listaTrabalhos['id_usuario']; ?>" name="token_trabalho[]" value="<?php echo $row_listaTrabalhos['token_submissao']; ?>" />
   <input type="hidden" id="marcadorStatus<?php echo $row_listaTrabalhos['id_usuario']; ?>" name="marcadorStatus[]" value="<?php echo $row_listaTrabalhos['status_trabalho']; ?>" />
   <input type="hidden" id="nome<?php echo $row_listaTrabalhos['id_usuario']; ?>" name="nome[]" value="<?php echo $row_listaTrabalhos['nome_usuario'] . " " . $row_listaTrabalhos['sobrenome_usuario']; ?>" />
   <input type="hidden" id="email_usuario<?php echo $row_listaTrabalhos['id_usuario']; ?>" name="email_usuario[]" value="<?php echo $row_listaTrabalhos['email_usuario']; ?>" />

    <?php } while ($row_listaTrabalhos = mysqli_fetch_assoc($listaTrabalhos)); ?>
  </form>

marcadorStatus[] it's my anchor to compare with the select input status_trabalho[].

I would like to serializeArray() with something like marcadorStatus[] != status_trabalho[]. So, if it's true, submit only the group of inputs with the same array index (id_autor[], nome[], email_usuario[] and so on).

Community
  • 1
  • 1

1 Answers1

0

I'm thinking you do not need the input elements to all have id values. If not, it is better to leave them off; It's less work for the browser.

Instead give them a class that groups them.

Now I don't program PHP, so you might have to consider this as pseudo-code:

<form method="post" action="" id="resultado_trabalhos" name="resultado_trabalhos">
<? $i = 0; ?>
<?php do { ?>
    <select class="group-<?php echo $i; ?>" name="status_trabalho[]" data-group-index="<?php echo $i; ?>">
        <option value="3" <?php if (!(strcmp(3, $row_listaTrabalhos['status_trabalho']))) {echo "selected=\"selected\"";} ?>>em avaliação</option>
        <option value="4" <?php if (!(strcmp(4, $row_listaTrabalhos['status_trabalho']))) {echo "selected=\"selected\"";} ?>>oral</option>
        <option value="5" <?php if (!(strcmp(5, $row_listaTrabalhos['status_trabalho']))) {echo "selected=\"selected\"";} ?>>poster</option>
        <option value="6" <?php if (!(strcmp(6, $row_listaTrabalhos['status_trabalho']))) {echo "selected=\"selected\"";} ?>>não selecionado</option>
    </select>

    <textarea class="group-<?php echo $i; ?>" name="motivoDevolucao[]" cols="100" rows="2" wrap="physical" style="display:none;">&nbsp;</textarea>

    <input type="hidden" class="group-<?php echo $i; ?>" name="id_autor[]" value="<?php echo $row_listaTrabalhos['id_usuario']; ?>" />
    <input type="hidden" class="group-<?php echo $i; ?>" name="token_trabalho[]" value="<?php echo $row_listaTrabalhos['token_submissao']; ?>" />
    <input type="hidden" class="group-<?php echo $i; ?>" name="marcadorStatus[]" value="<?php echo $row_listaTrabalhos['status_trabalho']; ?>" />
    <input type="hidden" class="group-<?php echo $i; ?>" name="nome[]" value="<?php echo $row_listaTrabalhos['nome_usuario'] . " " . $row_listaTrabalhos['sobrenome_usuario']; ?>" />
    <input type="hidden" class="group-<?php echo $i; ?>" name="email_usuario[]" value="<?php echo $row_listaTrabalhos['email_usuario']; ?>" />

    <? ++$i; ?>
<?php } while ($row_listaTrabalhos = mysqli_fetch_assoc($listaTrabalhos)); ?>
</form>

As for the ajax call, I think you want to use .serialize(), instead of .serializeArray().

$('#resultado_trabalhos').submit(function(e) {
    e.preventDefault();

    var $form = $(this);

    // This will hold the input elements to be submitted.
    var inputs = [];

    // Add any inputs that should always be submitted.
    inputs.push.apply(inputs, $form.find('.always-submit').get());

    // Loop through all the "status_trabalho[]" selects.
    $form.find('select[name="status_trabalho[]"]').each(function() {
        var $select = $(this);
        // Check if the select value was changed.
        if ($select.val() != $select.nextAll('input[name="marcadorStatus[]"]:first').val()) {
            // Push all the inputs from the group onto the "inputs" array.
            inputs.push.apply(inputs, $form.find('.group-' + $select.attr('data-group-index')).get());
        }
    });

    $.ajax({
        type: 'post',
        url: 'someUrl',
        data: $(inputs).serialize(),
        // ...
    });
});
John S
  • 21,212
  • 8
  • 46
  • 56
  • I don't know why, but I'm getting error in this line `inputs.push.apply(inputs, $form.find('.group-' + $(select.attr('data-group-index')).get());` – Renato Pirei Apr 28 '15 at 02:11
  • @RenatoPirei - There was an extra opening parenthesis on that line. It should be `$select.attr`, not `$(select.attr`. I have updated the code in my answer. – John S Apr 28 '15 at 02:21
  • Now it's returns an empty string... `$(inputs).serialize( )` . I'm testing with `var test = $(inputs).serialize( )` and showing with `console.log(test)` – Renato Pirei Apr 28 '15 at 03:17
  • @RenatoPirei - I create this [jsfiddle](http://jsfiddle.net/bteq51sx/) to test the code, and it appears to work. Does your generated HTML match what is in the jsfiddle? – John S Apr 28 '15 at 03:20
  • I'm testing right now. But, I have another doubt. If I have to send other inputs that are not part of this groups... like an input that holds the area to update this fields on DB? – Renato Pirei Apr 28 '15 at 03:28
  • Can I call the action with `jQuery('#resultado_trabalhos').submit(function(){});`? I have to press a button to submit my form... – Renato Pirei Apr 28 '15 at 03:33
  • @Renato Pirei - You could add those elements to the `inputs` array too. I think the easiest way would be to add a class named `always-submit` to them. Then you can add them like this `inputs.push.apply(inputs, $('.always-submit`).get());`. – John S Apr 28 '15 at 03:33
  • @Renato Pirei - Since you are submitting via ajax, you could just make the ajax call when the button is clicked. Or you could use a "submit" button and bind a submit-event handler to the form that makes the ajax call (and prevents the normal form submission). – John S Apr 28 '15 at 03:37
  • @Renato Pirei - I updated my answer to include the code for the extra inputs and to show what it looks like to bind the submit-event handler. Now the ajax call will be made if the user clicks a "submit" button. – John S Apr 28 '15 at 03:44
  • there is something wrong in the loop. There is a Syntaxe error. – Renato Pirei Apr 28 '15 at 03:54
  • It's almost working. With the static example (and static inputs) works fine... but, something goes wrong when the inputs are dynamic. Maybe is something with the DOM? – Renato Pirei Apr 28 '15 at 04:12
  • @RenatoPirei - Compare the elements generated by PHP with those in the jsfiddle, there may be a difference. – John S Apr 28 '15 at 04:34
  • @RenatoPirei - Glad it works. You can click the check mark next to the answer to indicate your issue was resolved. – John S Apr 28 '15 at 04:46