Help with JQuery + Php

-2

I have a form that is is loaded through a while from bd. the structure is this:

<form id="formulario" name="formulario" action="javascript:Func()" method="post"> 
  <div id="div_prod"> 
    <?php $sql_prod = mysql_query("SELECT bla bla bla"); 
    while($sql= mysql_fetch_object($sql_prod )) { ; ?>  
      <input name="a" id="a" type="hidden" value="<?php echo $sql->a; ?>">
      <input name="b" id="b" type="hidden" value="<?php echo $sql->b; ?>">
      <input name="c" id="c" type="hidden" value="<?php echo $sql->c; ?>">
      <button type="submit"><?php echo $sql->a; ?></button> <?php }; ?>
  </div>
</form>

Either the name inputs a, b, and c are generated n times, depending on the number of records returned from the select.

So, I have a function that adds the results in mysql, as below.

$("#formulario").submit(function() {
        var a = $("#a").val();
        var b = $("#b").val();
        var c = $("#c").val();
        $.post('../scripts/arquivo.php', {a: a, b: b, c: c}, function(resposta){ 
       if (resposta != false) {
            $("#status").html(resposta);
       } else {
            $("#status").html("Inserção realizada com sucesso!");
       }
});

The process is working fine, however, it always loads only the first while.

I have tried to put the variables with name and id [] (eg, but it does not send anything and it does not give any error.

Leaving without the [] only sends the first item.

How do I resolve this?

    
asked by anonymous 25.05.2017 / 02:19

2 answers

0

The id property will only be used in the browser, but name sim will be used to mount the request data, so you have to pass as an array, for example: name = "a[]" .

But note that your server-side PHP script, which is receiving this request to save to the database, needs to be changed to receive the parameters in array format.

Edit

For manipulating the id with jquery, you can dynamically define the ids according to a reference value, which can be an index, within the while you would do something like this:

$i++;
<input name="a" id="a<?php echo '_' . $i; ?>" type="hidden" value="<?php echo $sql->a; ?>">
<input name="b" id="b<?php echo '_' . $i; ?>" type="hidden" value="<?php echo $sql->b; ?>">
<input name="c" id="c<?php echo '_' . $i; ?>" type="hidden" value="<?php echo $sql->c; ?>">
    
25.05.2017 / 14:04
0

Edit: If you repeat the id of the arrais n times, it will only catch the first time.

The problem is that you are passing to ajax, only the values of the inputs with ids a, b, and c. Then he will only play the first 3. You can do this:

$("#formulario").submit(function() {
    var dados = $(this).serialize(); //pega todos os inputs e transforma em query string
    $.post('../scripts/arquivo.php', dados, function(resposta){ 
   if (resposta != false) {
        $("#status").html(resposta);
   } else {
        $("#status").html("Inserção realizada com sucesso!");
   }
});

In your html you can follow the example of Ademir Mazer, and treat the values as you see fit in your scripts / php file:

<input name="a" id="a<?php echo '_' . $i; ?>" type="hidden" value="<?php echo $sql->a; ?>">
<input name="b" id="b<?php echo '_' . $i; ?>" type="hidden" value="<?php echo $sql->b; ?>">
<input name="c" id="c<?php echo '_' . $i; ?>" type="hidden" value="<?php echo $sql->c; ?>">
    
25.05.2017 / 21:15