Hello, please tell me how to get the value from the hidden field html

<input type = 'hidden' name = 'id'> 

Suppose I have 10 entries in the database, and there is an ID for each entry, and when I display, I add a button to the output information:

 <form action = "" method = "post" name = "delete"> <input type = "hidden" name = "id" value = "<?=$id?>"> Логин пользователя: <input type = "text" name = "username" value = "<?=$username?>"> <input type = "submit" value = "Удалить пользователя"> </form> 

So from this list, when I clicked on the Delete user button, I would delete exactly the one opposite to which this button was, I tried to do this:

 $("form[name='delete']").submit(function(){ var id = $("input[name = 'id']").val(); $.post("ajax.php" ,{id:id, action:"delete"}, function(){ alert("Пользователь удалён"); }); return false; }); 

But the problem is that the ID that I take from the hidden field is always the same, and that is displayed normally:

  1. admin
  2. user
  3. banned
  4. moder

And everyone has their own ID, but when I click on the button, only the very first ID in the list of users is taken.

 <?php $sql = mysql_query("SELECT * FROM tb_company WHERE active = '1'"); if(mysql_num_rows($sql) > 0){ while($res = mysql_fetch_assoc($sql)){ $id = $res["id"]; $allBuy = mysql_query("SELECT * FROM tb_user_stock WHERE id_company = '$id'"); $allBuy = mysql_num_rows($allBuy); echo " <tr style = 'text-align: center; border-bottom: 2px solid black;' class = 'td_link'> <form action = '' method = 'post' name = 'buy_form'> <input type = \"hidden\" name = \"id\" value = \"$id\"> <td>" .$res["name"] ."</td> <td>" .$res["price"] ."</td> <td>" .$res["percent"] ."</td> <td>" .$res["summ"] ."</td> <td>$allBuy</td> <td><input type = 'text' name = 'count' size = '4' value = '1'></td> <td><input type = 'submit' name = 'buy' value = 'Купить'></td> </form> </tr> "; } 

}?>

    3 answers 3

    Assign a unique id attribute to each hidden field. But in general this is done somehow like this:

     <label> <input type="radio" name="deleteUser" value="<?=$id?>" /> Удалить Васю </label> 

      Well, because this code

       $("input name = [name = 'id']").val() 

      it’s not at all clear what it chooses from your HTML, most likely the result is null and PHP deletes the record with id = 0 . I'm not sure about that, but most likely it is.

      The id must be searched in the context of the submitted form (only the second line has changed):

       $("form[name='delete']").submit(function(){ var id = $(this).find("input[name='id']").val(); $.post("ajax.php" ,{id:id, action:"delete"}, function(){ alert("Пользователь удалён"); }); return false; }); 
      • $ ("input name = [name = 'id']"). val (); Oh, sorry, here is $ ("input [name = 'id']"). Val (); - Angus123

      Everything is taken correctly from you. form[name='delete'] you have exactly as many as users. The solution may be the following.

       $("form[name='delete']").submit(function(){ var id = $(this).find("input[name = 'id']").val(); $.post("ajax.php" ,{id:id, action:"delete"}, function(){ alert("Пользователь удалён"); }); return false; }); 
      • Well then it outputs: undefined - Angus123
      • And if I have a lot of forms and they are displayed in a loop? Ie something like this (see the very first post) - Angus123
      • so where 2 work and 100 work, I gave you an example in jsfiddle and look there everything is fine and then comment on what you draw there in a cycle. You 2 people at the same time gave the same answer. - binliz
      • I looked and checked on the html page, everything works like a clock, but nothing works on php. It constantly displays: undefined, I haven’t even gotten this Js script to work ... And even if AFTER the php script, make a separate form (without and so on, as you have in the example), then the code works ... - Angus123