There are several consecutive forms.

<form> <input type="hidden" name="i" value="1"> <input type="submit" value="ok"> </form> <form> <input type="hidden" name="i" value="2"> <input type="submit" value="ok"> </form> 

Using ajax and jquery, a POST request is sent, sending only 1 value from the hidden i field, the question is how to choose this value depending on the form in which you clicked on the ok button. Or, what is even more interesting is how to do the same, only without a form, for example, by clicking on the link.

    2 answers 2

    hang up the processor on click on the link in which you send request, namely the value of only one field. The answer lies in the question itself. You can do the following: give the names of hidden fields almost the same as the links with the only difference - in the beginning add the letter h. Then, after clicking on the link, we will determine the name of the clicked link and form the name of the corresponding hidden field programmatically. The sending code is like this:

     <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('#error').hide(); var hiddenname=''; $("#container").click(function (e) { if (e.target.id!='') {hiddenname = e.target.id; hiddenname='h'+hiddenname;} var x = $(hiddenname).val();//получаем в переменную х значение из скрытого поля $.ajax({ type: "POST", url: "test.php", data: "x="+x, success: function(html){ $("#error").append(html); $("#error").slideDown('slow'); } }); }); }); </script> </head> <body> <div id="error"></div> <div id="container"> <form> <a href="#" id="link1">link1</a> <input type=hidden id="hlink1"><br><br> <a href="#" id="link2">link2</a> <input type=hidden id="hlink2"><br><br> <a href="#" id="link3">link3</a> <input type=hidden id="hlink3"><br><br> <a href="#" id="link4">link4</a> <input type=hidden id="hlink4"><br><br> </form> </div> 

    And then on the server you take out the value of the variable x from the POST array

    • This option works only for the first form, all the others do not respond to the click, this option is not very accurate. - Sharp - eyed
    • once again - you have several links. For each of them is a hidden field. Depending on which link you clicked, you need to send the appropriate field. I understood correctly? - ikot
    • That's right. - Sharp - eyed
     <form class="add"> <input type="hidden" name="i" value="1"> <input type="submit" value="ok"> </form> <form class="add"> <input type="hidden" name="i" value="2"> <input type="submit" value="ok"> </form> $(function() { $("form.add").submit(function(){ $.post("test.php", $(this).serialize(), function(data){ }); return false; }); }); 

    For each form, it was only necessary to add a class.

    • Your decision does not solve the problem that you set for yourself. Using your code, data from all forms with the add class will be sent to the server. See above solution - I have already fixed it - ikot
    • Well, how it does not solve? You carefully read the first sentence. As for the option with links, to pride the garden from the heap id for each link the form field is not very accurate. For all links, you can use one id, and in the link itself, write an additional parameter with a value and transmit it, a very beautiful and simple solution. - Sharp - eyed
    • this is also an option, but I’d argue about the id heap - your method is no better - you create a bunch of forms, and this is even worse. - ikot