1

I have a problem, I have a field with several forms, these forms can get to repeat up to 50 times but with different values​​, I want to send the form you select from the list with $. post ('process.php', $ ("# form") . serialize (), function (data), but when clicking on a form values ​​are sent only the first form but not send form values ​​to select.

try units themselves, those are two simple documents such

forms.html

<html>
<head>
<title>Ejemplo de form con jquery</title> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script type="text/javascript">
$(function() {
$("form").validate( {
submitHandler: function(form) {
$.post('process.php', $(form).serialize(), function(data) {
$('#results').html(data);
            });
        }
    });
});
</script>
</head>
<body>
<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>

    <input type="submit" name="submit" value="SEND"> 
</form>

<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
  <input type="submit" name="submit" value="SEND"> 
</form>

<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
  <input type="submit" name="submit" value="SEND"> 
</form>

<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
  <input type="submit" name="submit" value="SEND"> 
</form>

<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
  <input type="submit" name="submit" value="SEND"> 
</form>
<div id="results"></div>
</body>
</html>

process.php

 <?php

    print "Form enviado correctamente: <br>the post value is <b>".$_POST['name']."</b> ";
?>

all I want is that if I put a value to the last form below and I will send you send the value of this form or any you choose, not the value of the form above that there is where the problem is and do not want q send the value of all forms at once ls just want to send the value of the form to select

Barmar
  • 741,623
  • 53
  • 500
  • 612
Deviandorx
  • 61
  • 1
  • 1
  • 8
  • 1
    Can you please make your question more clear? – Surreal Dreams Aug 15 '13 at 02:06
  • 1
    You can't have multiple forms with the same ID. IDs have to be unique. – Barmar Aug 15 '13 at 02:12
  • I have a field with several forms, these forms can get to repeat up to 50 times but with different values​​, I want to send the form you select from the list with $. post ('process.php', $ ("# form") . serialize (), function (data), but when clicking on a form values ​​are sent only the first form but not send form values ​​to select – Deviandorx Aug 15 '13 at 02:16
  • Even if you're generating the form in a loop, you should use a counter variable and give them IDs like `form1`, `form2`, etc. Or give them a common class. But HTML elements can't share IDs, IDs are by definition unique identifiers. Your jQuery selector is matching the first, then not expecting to find anymore, so it stops there. – nbrooks Aug 15 '13 at 02:31
  • You probably need newer versions of [jQuery](http://api.jquery.com/category/version/1.9/) and the [validate plugin](http://jqueryvalidation.org/). You should also read the docs on the [`serialize`](http://api.jquery.com/serialize), since it is probably not coming back in the format you're expecting. – nbrooks Aug 15 '13 at 02:59
  • all I want is that if I put a value to the last form below and I will send you send the value of this form or any you choose, not the value of the form above that there is where the problem is and do not want q send the value of all forms at once ls just want to send the value of the form to select – Deviandorx Aug 15 '13 at 03:09
  • hey help me http://stackoverflow.com/questions/18256775/get-location-href-in-a-function-load – Deviandorx Aug 15 '13 at 22:08

2 Answers2

0

Do not provide the same id to all the forms. You can always serialize each form's data with $('#formid').serialize(); And then, you can send it via an Ajax POST.

Or, you can omit the "id" completely, and loop through each form and access it's data using $(this). Since you are trying to send all forms' data at once, you can have a in each form. And then do this.

$(document).ready(function(){ $.each($("form"), function() { 
     $(this).validate({  submitHandler: function() {

            $.post('process.php', $(this).serialize(), function(data) {

                    $(this).find('.result').html(data);
                });
        }
     }); });

});
Sasanka Panguluri
  • 3,058
  • 4
  • 32
  • 54
  • the problem is that these forms are in php while and obviously they all have the same id, but each form has a value different post, but select the one you select only sends the value of the first form in the list and I seo to send the form value to select – Deviandorx Aug 15 '13 at 02:19
  • friend, sure you understood the function? because that code you sent me does not work, do not reflect anything in the results div – Deviandorx Aug 15 '13 at 02:28
  • I just updated the question, there have 2 examples documents, use them and you know what I mean. – Deviandorx Aug 15 '13 at 02:42
0

The submitHandler function receives the form being submitted as its argument, so you can do:

  $(function() {
      $("form").each(function() {
          $(this).validate( {
              submitHandler: function(formbeingsubmitted) {
                  $.post('process.php', $(formbeingsubmitted).serialize(), function(data) {
                      $('#results').html(data);
                  });
              }
          });
      });
  });

Apparently the jquery-validate plugin does not work properly when applied to a jQuery collection. So it's necessary to use .each() to initialize each of them separately.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • the problem is that these forms are in php while and obviously they all have the same id, but each form has a value different post, but select the one you select only sends the value of the first form in the list and I seo to send the form value to select – Deviandorx Aug 15 '13 at 02:31
  • I don't understand what you're saying. I don't do anything with the ID, so the fact that they're the same is irrelevant in my answer. You should change your PHP to give them different IDs (increment a number and append it to the ID) or not assign any ID at all if it's not really needed (it usually isn't in cases like this). – Barmar Aug 15 '13 at 02:34
  • I just updated the question, there have 2 examples documents, use them and you know what I mean. – Deviandorx Aug 15 '13 at 02:41
  • Does it work if you remove all the `id="form"` attributes? – Barmar Aug 15 '13 at 02:46
  • no, does not work, use the code that came up, there is complete and test the example, you will see what I want to accomplish – Deviandorx Aug 15 '13 at 03:20
  • See the updated code, I've tested this and it works. – Barmar Aug 15 '13 at 03:45
  • excellent friend, now if I want to change the id to forms that are eg id = formal as write in the code? – Deviandorx Aug 15 '13 at 03:59
  • I don't understand what you're saying. The ID isn't used for anything, get rid of it. – Barmar Aug 15 '13 at 04:00
  • Friend the id of the form of the example is 'form method = "post" id = "form"', now I want the jquery function with form method = "post" id = "formelm" as simple as that. – Deviandorx Aug 15 '13 at 04:07
  • Use `$("#formelm")` to target a specific form with that ID. That's basic jQuery. – Barmar Aug 15 '13 at 04:10
  • how to add the effect $ (".") hide ("slow"), to the function? – Deviandorx Aug 15 '13 at 04:22
  • `$(".")` is not a valid selector. Maybe `$(form).hide("slow")` is what you're looking for? – Barmar Aug 15 '13 at 04:25
  • only be hidden form that submits and not all at once hide – Deviandorx Aug 15 '13 at 04:29
  • Maybe you're confused because my variable name is the same as the element tag. I've changed my answer, so now it should be `$(formbeingsubmitted).hide("slow")`. Does this make it clearer? – Barmar Aug 15 '13 at 04:32
  • hi! i have a new question! http://stackoverflow.com/questions/18256359/how-to-add-effect-hide-slow-to-div-inside-a-php-while – Deviandorx Aug 15 '13 at 15:45