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:
- admin
- user
- banned
- 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> "; }
}?>