Jump to content


Photo

Help with getting ID from a form in While loop to my AJAX

ajax php jquery

  • Please log in to reply
2 replies to this topic

#1 piehead

piehead

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 14 February 2013 - 08:27 PM

I have been stuck on this for 3 days... I would love the experts to shoot me some words of advice, appreciate it.

I have a form I create from a while loop in php. I can submit it if I name the form and place that in the AJAX but it just uploads the last record in the table not the one I actually click. I know I need a unique ID from the form, which I have but I can get it in the AJAX ;( I have used $(this).attr("id") , $(this).form("id") etc and nothing gets it in.. Any advice would be great

MY PHP Loop

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>';
    
 
  }

My AJAX
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>


<script type="text/javascript">
$(document).ready(function() {
<!--$('#load').hide();-->
});


$(function() {
$(".approve").click(function() {
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>


#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 14 February 2013 - 08:31 PM

Your unique id should be a hidden input field.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 piehead

piehead

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 14 February 2013 - 08:34 PM

How do I pull that in my ajax?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com