Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2013, 10:42 PM   PM User | #1
piehead
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
piehead is an unknown quantity at this point
Issue Grabing ID from form in a while loop using JQuery/AJax

I am having such a hard time with this.. 3 days stuck on something Im sure you the experts would laugh at.... so any help would be wonderful.

I have a php loop that loads a form for each row in a table. I want to approve a record and save it to a table. Problem is I cant seem to pull the unique ID of the form, it always adds the last row in the table.. I have tried $(this).attr("id"), $(this).form("id") and no dice. Please help a tired novice lol

The Form Loop (php)
Code:
while($row = mysql_fetch_array($pendingresult))
 
  { 
 $id = "myForm".$row['reg_id'];
 echo '<table width="100%" border="0" cellspacing="0" cellpadding="0" >';
  print "<form id=\"$id\" name=\"CDs\" method=\"post\" >";
  
    echo '<tr class="commentContainer" style="color:#FFF">';
    echo"<td><input type=\"text\" name=\"team_name\" value=\"$row[team_name]\"</td>";
	
    echo"<td><input type=\"text\" name=\"reg_id\" value=\"$id\"</td>";
    echo"<td><input type=\"text\" name=\"team_level\" value=\"$row[team_level]\"</td>";
    echo"<td><input type=\"text\" name=\"notes\" value=\"$row[comments]\"</td>";
   
    echo"<td>";
	
	 
   	
	echo "<td class=\"delete\" align=\"center\" id=".$row['reg_id']." width=\"10\"><a href=\"#\" id=\"$row[reg_id]\"><img src=\"admin/images/delete.png\" border=\"0\" ></a></td>";
    echo "<td class=\"approve\" align=\"center\" id=".$id." width=\"10\"><a href=\"#\" ><img src=\"admin/images/approve.png\" border=\"0\" ></a></td>";
	
	echo "</td>";
	
  echo"</tr>";
  echo "</form>";
  echo ' </table>';
    
 
  }
The AJAX
Code:
<!---  Pending DB Approve Request----->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
});

$(function() {
$(".approve").click(function() {
<!--$('#load').fadeIn();-->
var commentContainer = $(this).parent('tr:first');
var id = $(this).attr("id");
var string = 'id='+ id;
var formData = $(this).attr("id")

$.ajax({
   type: "POST",
   url: "approve.php",
   data: $(formData).serialize(),
   
   cache: false,
   success: function(){
	commentContainer.slideUp('slow', function() {$(this).remove();});
	
  }
   
 });

return false;
	});
});
</script>
piehead is offline   Reply With Quote
Old 02-15-2013, 07:18 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
id means "identity". What would you think if someone told you that you share your identity with others? You'd say: "No way, my identity is unique". And the same is true also for Javascript.

Bottom line: You should not use the same id attribute value on more than one element on the same page.

Workaround: Prepend a unique String to the numeric identifier, like "article_101" and "cell_101". Then you can extract the numeric part using
Code:
var numID = Number($(this).attr('id').match(/(\d+)/)[1]);
devnull69 is offline   Reply With Quote
Old 02-15-2013, 07:25 PM   PM User | #3
piehead
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
piehead is an unknown quantity at this point
Each form has a unique ID... $id = "myForm".$row['reg_id'];

thanks ok right?
piehead is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:26 PM.


Advertisement
Log in to turn off these ads.