Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have a form in which i am getting value form database.
Now i add a delete button and want to add a function by which it delete using jquery code.
But unable to do this.
It only showing the first value.

PHP
<?php
$i=0;
$total_amount=0;
while ($i < $num)
 {
$f0=mysql_result($query_for_result,$i,"empta_id");
$f1=mysql_result($query_for_result,$i,"department");
$f2=mysql_result($query_for_result,$i,"username");
$f3=mysql_result($query_for_result,$i,"amount");
?>
<tr valign="top"  >
<!--<td style="border-removed#cdcdcd solid; border-bottom-width:1px;"><?php echo $f0; ?></td>-->
<td style="border-removed#cdcdcd solid; border-bottom-width:1px;"><?php echo $f1; ?></td>
<td style="border-removed#cdcdcd solid; border-bottom-width:1px;"><?php echo $f2; ?></td>
<td style="border-removed#cdcdcd solid; border-bottom-width:1px;"><?php echo $f3; ?></td>
<!--<td style="border-removed#cdcdcd solid; border-bottom-width:1px;"><a href="delete-ta-info.php?empta_id=<?php echo $f0; ?>">Delete</a></td>-->
<td style="border-removed#cdcdcd solid; border-bottom-width:1px;"><a href="" id="del" name="del"  önclick="delete_ta();">Delete</a></td>
<td style="border-removed#cdcdcd solid; border-bottom-width:1px;" ><input type="hidden" id="empta" name="empta" value="<?php echo $i; ?>"/></div></td></tr>

<?php
$i++;

}
?></table>
<script>
function delete_ta()
{
var empta_id=$('#empta').val();
alert(empta_id);
}
</script>


Can anybody have any idea? where i am going wrong? and if i should use indexing here then please help me with example with code.

Now I am able to get the index value like this...
HTML
<td style="border-removed#cdcdcd solid; border-bottom-width:1px;" ><input type="hidden" id="empta<?php echo $i;?>" name="empta<?php echo $i;?>" value="<?php echo $i; ?>"/></td>


Now only problem is how to access this value in jquery function.


thanks
Posted
Updated 12-Feb-13 0:26am
v3

The main reason why your approach does not work, that you cannot have more HTML elements with same ID (empta)!
You need to append $i after empta_ string (new ids will be empta_1, empta_2 and so on...).
Your delete_ta js function should receive id parameter like this:
JavaScript
function delete_ta(id)
{
var empta_id=$('#empta_'+id).val(); // find unique named elem
alert(empta_id);
}

Or simply, you can ignore passing of id ($i) parameter and use jQuery traversing - find clicked element parent and work with that...
 
Share this answer
 
Comments
[no name] 12-Feb-13 4:37am    
Thanks
Matej Hlatky.
that's what i am talking about (indexing).
If you have time can you help me with some brief code for both methods.
thanks
[no name] 12-Feb-13 6:02am    
Hi,
Traversing is not working because the delete button is inside the table.
Thanks
hi,
As i see it is not possible by jquery because it always take other value then i decide to do it with php.
Add code in Main
PHP
if(isset($_REQUEST['msg_del']))
{
$msg_del=$_REQUEST['msg_del'];
if($msg_del=="Success")
{
echo "<script>alert('Successfully Delete!');</script>";
}
}

Use this link
<table><tbody><tr><td style="border-removed#cdcdcd solid; border-bottom-width:1px;"><a href="delete-ta-info.php?empta_id=<?php echo $f0; ?>">Delete</a></td></tr></tbody></table>

Send in other page "delete-ta-info.php" add Redirect
PHP
header("Location:http://www.paperbind.in/employee_portal/admin/edit-ta.php?msg_del=Success");

The main reason to do this is i want to show a message without going to other page and in my other page i use jquery for this and in this page i found problem to do this by jquery
then i use this method.
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900