1

I want to add confirm box by using JavaScript. So, here is the code that I have used. When I click on the Delete button, it shows me a Confirmation message and when I click Ok, it doesn't delete my row.

What is wrong with this? How can I fix this?

a table

Here is the Index Page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
    <!-- Bootstrap core CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="row"> <!-- Start Of The Row Class -->
<div class="col-md-8 col-sm-4 hero-feature"> <!-- Start Of The Col Class -->
<br><br>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
include('ksdb.php');
// get results from database
$result = mysql_query("SELECT * FROM academic")
or die(mysql_error());
?>
<table class='table table-bordered'>
<tr class='danger'> 
<th>ID</th> 
<th>Name</th> 
<th>Username</th> 
</tr>
<?php
// display data in table
while($row = mysql_fetch_array( $result )) {
	?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><a href="AdminChangePw.php?id=<?php echo $row['id']; ?>" alt="edit" >Change</a></td>
 <td><a href="AdminEdit.php?id=<?php echo $row['id']; ?>" alt="edit" >Edit</a></td>
 <td><input type="button" onClick="deleteme(<?php echo $row['id']; ?>)" name="Delete" value="Delete"></td>
</tr>
<script language="javascript">
 function deleteme(delid)
 {
 if(confirm("Do you want Delete!")){
 window.location="AdminDel.php?id=' +id+'";
 return true;
 }
 } 
 </script>
<?php
}
?> 
 </table>
</div> <!-- End Of The Col Class -->
</div> <!-- End Of The Row Class -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></script>
</body>
</html>

2
  • Is it removed from database? Commented May 28, 2017 at 10:37
  • @AdhershMNair - No.. Commented May 28, 2017 at 15:17

3 Answers 3

1

Change the id to 'delid'. You are calling the function as deleteme(delid). So You Must use delid, instead of id.

 function deleteme(delid)
 {
 if(confirm("Do you want Delete!")){
 window.location.href="AdminDel.php?id="+delid; /*Change id to 'delid'*/
 return true;
 }
 } 
Sign up to request clarification or add additional context in comments.

3 Comments

I think there shouldn't be single quotes around delid. OP is validating ids with is_numeric($_GET['id']) so an id with single quotes won't be considered valid.
@AdhershMNair and Abhijit_Parida - Can I use this method to a link ?? echo '<td><a id="myLink" href="#" onclick="deleteme(<?php echo $row['id']; ?>)">Delete</a></td>';
I think you mean like this .. <a id="myLink" href="AdminDel.php?id=<?php echo $row['id']; ?>">Delete</a>
0

Use window.location.href Instead of window.location

1 Comment

But, What is the different between window.location.href and window.location ??
0

There are a couple of problems with your function:

function deleteme(delid) {
    if(confirm("Do you want Delete!")){
        window.location="AdminDel.php?id='+id+'";// this should be delid, also bad quotes, also you want to set the href, not the location
        return true;
    }
} 

Fixed:

function deleteme(delid) {
    if(confirm("Do you want Delete!")){
        window.location.href="AdminDel.php?id=" + delid; 
        return true;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.