Reputation: 49
i am trying to get parameter of href
in ajax.
<a href="index.php?del_id=<?php echo $c_id; ?>" class="delete" value="<?php echo $c_id ?>">Delete</a></li>
now i am trying to get del_id
in ajax
<script type="text/javascript">
$(".delete").click(function(){
var url = ($(this).attr('href'));
var id= getURLParameter(url,'del_id');
if(confirm('Are you sure to remove this file ?'))
{
$.ajax({
url: 'maincode.php',
type: 'GET',
data: {id: id},
success: function(data)
{
alert("file removed successfully");
},
error:function()
{
alert('Something is wrong');
}
});
}
});
</script>
but after adding getURLParameter
code is not getting execute further.please help me.
Upvotes: 2
Views: 584
Reputation: 215
You really don't need to overthink this, the parameter is already in the url :
---- href="index.php?del_id=<?php echo $c_id; ?>"
$(".delete").click(function(){
var url = ($(this).attr('href')); // this already has the id in it
if(confirm('Are you sure to remove this file ?'))
{
$.ajax({
url: 'maincode.php',
type: 'GET',
// remove this, the data is in the url -- data: {id: id},
success: function(data)
{
alert("file removed successfully");
},
error:function()
{
alert('Something is wrong');
}
});
}
});
On the server, there will be a get parameter called del_id if you need the parameter to be id, change the url to :
index.php?id=<?php echo $c_id; ?>
If you really want to have the data populated by code, use data parameters
<a href="#" data-del-id="<?php echo $c_id; ?>" data-url="index.php" >
// JAVASCRIPT SIDE:
var url = $(this).data('url'); // OR : $(this).attr('data-url');
var id = $(this).data('del-id'); // OR : $(this).attr('data-del-id');
I hope this makes sense
Upvotes: 1