javascript - jQuery ajax script wont work with n number of forms from PHP -


i have little problem ajax jquery script , n number of forms...to more precise, php script generate n number of forms (form include 1 textarea , 1 button), , in head tag included jquery script. problem jquery work first form , not others (second, third...). needed work forms...this code:

<script>     $(document).ready(function() {     $("#submitform").click(function() {         var text = $("#comment").val();         var id = $("#id").val();          $.ajax(             {                 url: "addcomment.php",                 type: "post",                 data: "t="+ text +"&id="+id,                 success: function(data)                 {                     alert(data);                 }              });     });     });  </script> 

and php code

for($i=0; $i<$num; $i++) {      echo "<div style='border: 1px solid black;'>                <textarea id='comment'></textarea>               <input type='hidden' id='id' value='".$id."'/>               <input type='button' id='submitform' value='add comment'>                </div>"; } 

what problem???

on php side should change similar ensure html elements has unique id.

for($i=0; $i<$num; $i++) {      echo "<div style='border: 1px solid black;'>                <textarea id='comment".$i."'></textarea>               <input type='hidden' id='id".$i."' value='".$id."'/>               <input type='button' id='".$i."' class='submitform' value='add comment'>                </div>"; } 

and change javascript similar reflect changes made on php side

<script>     $(document).ready(function() {     $(".submitform").click(function() {         var formnumber = $(this).attr("id"); // form number clicked, id attribute of clicked button         var text = $("#comment"+formnumber).val(); // comment of particular form         var id = $("#id"+formnumber).val(); // id of particula form          $.ajax(             {                 url: "addcomment.php",                 type: "post",                 data: "t="+ text +"&id="+id,                 success: function(data)                 {                     alert(data);                 }              });     });     });  </script> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -