Delete Confirmation when click on link - php

I have this code inside a while loop for fetching data from the database. I want to show a confirmation popup when the Delete link is clicked.
echo "<td><a href='delete.php?id=" . $row['serial_no'] . "'>Delete</a></td>";
How can this be done?

Try out confirm function from JavaScript, learn jQuery as fast as possible and try to seperate javascript from html ;).
http://www.w3schools.com/jsref/met_win_confirm.asp
<td>
<a href="delete.php?id=<?php echo $row['serial_no'] ?>" id="a_id">
Delete
</a>
</td>
<script type="text/javascript">
$(function() {
$('td a#a_id').click(function() {
return confirm("Are You sure that You want to delete this?");
});
});
</script>

add onClick attribute for anchor link and call the function on it. Or simply show confirm.
<a onClick = "confirm ('Are You Sure?')"
Complete Example
function disp_confirm()
{
var r=confirm("Press a button!")
if (r==true)
{
alert("You pressed OK!")
}
else
{
alert("You pressed Cancel!")
}
}
<a onclick="disp_confirm()"

try this
function DeleteClick(serial_no)
{
if(confirm('Are you sure to delete ' + serial_no + '?'))
{
alert('Data deleted');
return true;
}
return false;
}
echo "<td><a href='delete.php?id=" . $row['serial_no'] . "' onclick="return
DeleteClick(\''. $row['serial_no'].'\')">Delete</a></td>";

echo "<td>Delete</td>";

try this code,hope it will work-
<td>
<a href="#" onclick="conf("delete.php?id=".<?php echo $row['serial_no']; ?>.")">
Delete
</a>
</td>
<script type="text/javascript">
function conf(str) {
if(confirm("Your Message") == true){ location.replace(str);}
}
</script>
and in the delete.php page-just grab the id from url and run the delete query.

Related

How to echo a href links with onclick alert php

Problem
When i click this link its alert won't open what should I do ?
My Code
echo "<td><a href='#' onClick='alert('Not an Enumerator!')'>Location</a></td>";
You have to escape quotes \' when necessary, try this:
echo '<td>Location</td>';
Try By Following Example.
<a href="#" onClick='alert("Not an Enumerator!")'>Location</a>
Don't Use Single quote inside alert
Try below example you can do both alert and onclick or either based on return type of function called on click.
<?php
echo "<td><a href='test.php' onclick='return test_click();'>Location</a></td>";
?>
<script type="text/javascript">
function test_click(event){
alert("Inside this function");
return true;
}
</script>

redirect after click button delete

I have problem when I click button edit, I hope I can redirect to
http://localhost/activity/edit_activity/172
where 172 is activity_id but actually i redirect activity_detail so here my code
My HTML
<a href='<?php echo site_url();?>activity/delete_activity_edit/<?php echo $row->activity_detail_id;?>' onclick='return confirm()' class="btn btn-sm btn-danger">
<i class="glyphicon glyphicon-trash"></i>Delete</a>
My Controller
public function delete_activity_edit()
{
$id=$this->uri->segment(3);
$this->activity->remove($id);
redirect('activity/edit_activity/'.$id);
}
My Model
public function remove($id)
{
$where_array = array(
'activity_detail_id'=>$id);
$this->db->where($where_array);
$this->db->delete('t_trx_activity_detail');
}
Where is my wrong code. And how to resolve it?
You can use below code format:
<i class="glyphicon glyphicon-trash"></i>Delete
Add this javascript function:
<script type="text/javascript">
var url="<?php echo base_url();?>";
function confirm_delete(id){
var r=confirm("Do you want to delete this?")
if (r==true)
window.location = url+"activity/delete_activity_edit/"+id;
else
return false;
}
</script>
Now your controller redirection works.

Need to get id attribute

I have this php code to populate with links:
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $result)
{
if($result['tip']=='1')
{
$camere=" - ".$result['camere'];
}
else
{
$camere="";
}
$view[]="
<tr>
<td>".$result['id_anunt']."</td>
<td>".$result['den_tranzactie']."</td>
<td>".$result['den_prop'].$camere."</td>
<td>".$result['judet']." ".$result['oras']." ".$result['zona']."</td>
<td><a href='#' id='".$result['id_anunt']."' onclick='view();'>View</a> <a href='#' id='".$result['id_anunt']."'>Edit</a> <a href='#' id='".$result['id_anunt']."'>Arhivare</a> <a href='#' id='".$result['id_anunt']."'>special</a></td>
</tr>";
}
I need to get the id attribute for "View" link on click it with this function:
function view()
{
alert ($(this).prop('id'));
}
but I receive this: "object HTMLinputelemnt".
How can I get the id?
Thanks a lot.
Put a Class on your link.
Eg:
<a href="#" class="viewLink" id="....
Then in jquery:
$(".viewLink").click(function() {
var id = $(this).attr('id');
alert(id);
});
Modify your
onclick='view();'
to
onclick='view(this);'
And the view function as
function view(link)
{
alert ($(link).attr('id'));
}
use
$view[]="
<tr>
<td>".$result['id_anunt']."</td>
<td>".$result['den_tranzactie']."</td>
<td>".$result['den_prop'].$camere."</td>
<td>".$result['judet']." ".$result['oras']." ".$result['zona']."</td>
<td><a href='#' id='".$result['id_anunt']."' onclick='return view(this);'>View</a> <a href='#' id='".$result['id_anunt']."'>Edit</a> <a href='#' id='".$result['id_anunt']."'>Arhivare</a> <a href='#' id='".$result['id_anunt']."'>special</a></td>
</tr>";
Javascript
function view(element){
alert (element.id);
return false;
}
OR
alert ($(link).attr('id'));
Make sure you pass the link object as a parameter to view(), like this: onclick='view(this);'
Then in pure javascript:
function view(el)
{
alert(el.id);
}
"Don't use a canon to kill a mosquito". -- Confucius

Error in JavaScript confirm message

I am new to JavaScript. I cannot find the error of this code.
<td class="paid">
<a href="invoice.php?id=<?php echo $invoice_result['id']; ?>" onclick="confirm()">
<img src="images/view_3.gif"/>
</a>
</td>
<script type="text/javascript">
function confirm() {
if (confirm("Do you realy want do delete this vehicle?")) {
return true;
} else {
return false;
}
}
</script>
help me to find my error..
Please check with this
<td class="paid"><img src="images/view_3.gif"/></td>
<script type="text/javascript">
function confirmChk(){
if(confirm("Do you realy want do delete this vehicle?")){
return true;
}
else{
return false;
}
}
</script>
NB:The problem was with your function name I changed it confirm to confirmChk
You're trying to overload "confirm" function, which is built-in function and thus your code is not working. Change the name of your function from confirm to something else.
try this...
<td class="paid">
<span onclick="return confirm('Do you realy want do delete this vehicle?')">
<img src="images/view_3.gif"/>
</span>
</td>
Change the name of your function to something else than confirm.
Use this
onClick="return confirm('Do you realy want do delete this vehicle?');"
..
<td class="paid"><a href="invoice.php?id=<?php echo $invoice_result['id']; ?>"
onClick="return confirm('Do you realy want do delete this vehicle?');">
<img src="images/view_3.gif"/></a></td>
The error happened to you cos of the reserved word confirm.
chek this too Reserved words in javascript http://www.javascripter.net/faq/reserved.htm

yes no option with javascript delete function for a link

In 1stpage I have a table with full of data, and end of every row there is a delete link. When i click on that link, the link goes to another delete page where mysql delete query delete that particular data from database. That delete page redirect with the 1st page (location:1stpage). Now when i want to apply a javascript delete yes/no checking, every time it pops up with "Are you sure?" either i press yes or no the data is deleted from my table. I think the return false cannot working in this case. The code are in below. Please help me what should i do exactly.
Sorry for my bad english.
Thanks in advance.
1st page:
<?php
include "include.php";
?>
<script type="text/javascript">
function show_confirm()
{
var con = confirm("Are You Sure");
if (con ==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
<table>
<tr>
<td align="center">EDIT OR DELETE DATA</td>
</tr>
<tr>
<td>
<table border="1">
?php
if(isset($_POST['submit']))
{
$order = "UPDATE books SET author='$_POST[author]', title='$_POST[title]', price='$_POST[price]' WHERE isbn='$_POST[isbn]'";
mysql_query($order) or die (mysql_error());
}
$order = "SELECT * FROM books ORDER BY author" or die (mysql_error());
$result = mysql_query($order);
while ($row=mysql_fetch_array($result))
{
echo ("<tr>
<td>$row[isbn]</td>
<td>$row[author]</td>
<td>$row[title]</td>
<td>$row[price]</td>
<td> Edit </td>
<td> <a href=\"delete.php?isbn=$row[isbn]\" onclick='show_confirm()'> Delete </a> </td>
</tr>");
}
?>
</table>
</td>
</tr>
</table>
the page where mysql delete query run :
<?php header("Location: editordeletedata.php"); ?>
<p> <title>delete</title> </p>
<?php
include "include.php";
?>
<?php
if(isset($_GET['isbn']))
{
$order = "DELETE FROM books WHERE isbn = '$_GET[isbn]'";
mysql_query($order) or die (mysql_error());
}
?>
Look at this simple code snippet I wrote, should get you fixed up right.
<html>
<head>
<script type="text/javascript">
function show_confirm() {
return confirm("Are You Sure");
}
</script>
</head>
<body>
<a href="http://www.google.com" onclick='return show_confirm();'> Google </a>
</body>
</html>
Fiddle
I don't see any reason why return false will not work.
function show_confirm()
{
if (confirm("Are You Sure"))
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
return false;
}
}
Disable the delete link and move the redirection to the javascript function. The link should look like this:
<a href=\"javascript:void(0)\" onclick='show_confirm($row[isbn])'> Delete </a>
And the javascript function just like this:
function show_confirm(isbn)
{
if (confirm("Are You Sure"))
{
alert("You pressed OK!");
location.replace('delete.php?isbn='+isbn);
}
else
{
alert("You pressed Cancel!");
}
}

Categories