Help with Jquery + Php

Asked

Viewed 66 times

-2

I have a form that is loaded through a comic while. 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>

That is, the inputs of name a, b and c are generated n times, according to the amount of records that returns from select.

Ai, 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 ok, however, it always loads only the first while product.

I tried to put the variables with name and id [] (ex: but does not send anything and gives no error.

Leaving without the [] only sends the first item.

How do I fix it?

  • Post the query structure in PHP, probably the error is in it

  • Hello Marcelo. Thanks for the return. I just edited the post to make it easier to understand.

  • First hint, change the extension (obsolete) mysql for mysqli or pdo

  • Right. Changed. Thanks. This influence only in the case of different versions of PHP, correct?

  • Like this your code now?

  • Same thing.. I only changed mysql by mysqli

  • a good practice would be to use, instead of setting each input in a variable, the function serialize() jQuery, it would look something like this: var dados = $(this).serialize(); and put the name of your inputs as array. After that, just do the correct treatment in the file ../scripts/file.php to insert into the seat.

  • I have no idea how to do :/

Show 3 more comments

2 answers

0

The id property will only be used in the browser, but name yes will be used to mount the request data, for sure you need to pass as an array, in this case using, 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 the array format.

Edit

For handling the id with jquery, you can dynamically define the ids according to a reference value, which can be an index, inside 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; ?>">
  • Hello Friend. I have tried to do this and as for this, quiet. However, the question is whether you will have multiple inputs with id=a, which parameter I pass for jquery recognize the id I want, because regardless of which one I click, will always get the first id of while.

  • Okay, got it, look at the change in response

  • I made the suggested changes. Now, instead of sending only the first record, it sends everyone in the array. Need to trigger only id_8 data (for example).

  • There comes your logic of at the time of the ajax post capturing only the desired id, difficult to help with more code without knowing which decision rule to choose id

  • Forget my code then.. if possible, of course, give me an example of several Uttons and send only the button I click through Jquery. For me it works 100% but only if I have a single product on the list. To use serialize, what I realized is that it sends all the inputs of the page and not only what I client, which does not work in my case, because I only want 1 specific product of the listing and not all that are in the listing.

  • I won’t be able to set an example for you before Monday, but from what you described, another idea is to assemble a form for each Hidden item plus the corresponding button, still with the name indexes as explained, on this button or tag you trigger an onClick call to the function that will handle this request to the server.

  • Thanks friend for the support. I was able to solve with the steps of this link https://stackoverflow.com/questions/33380588/how-to-post-datafrom-php-while-loop-with-ajax

Show 2 more comments

0

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

The problem is pq vc is passing to ajax, only input values with ids a, b and c. So it will only interpret 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!");
   }
});

and in your html you can follow the example of Ademir Mazer, and treat the values the way you think best in your scripts/file.php:

<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; ?>">

Browser other questions tagged

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