Delete code did not work

Good day!
I have a javascript code for delete but it did not work.
here is my test1.php code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Machine 1 Problem</title>
<script type="text/JavaScript">
 
FUNCTION confirmDelete(){
VAR agree=CONFIRM("Are you sure you want to delete this file?");
IF (agree)
     RETURN TRUE ;
ELSE
     RETURN FALSE ;
}
</script>
</head>

<body>
<form method="post" action="machine_1.php" name="machine1">
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_name = 'db_machine1';

$query = mysql_connect("$db_host", "$db_user")or die (mysql_error());
$db = mysql_select_db("$db_name")or die (mysql_error());

$query = "SELECT * FROM tbl_machine1 ORDER BY Emp_ID ASC";
$result = mysql_query($query) or die(mysql_error());

echo "<table border='1'><tr>";
for($i = 0; $i < mysql_num_fields($result); $i++){
    echo "<th>".mysql_field_name($result, $i)."</th>";
    //echo "<th><a>.mysql_field_name($result, $i).""</a></th>";
}
echo "<th>Options</th>";
echo "</tr>";
while($row = mysql_fetch_array($result)){
    echo "<tr>";
    for($i = 0; $i < mysql_num_fields($result); $i++){
        echo "<td>". $row[$i] ."</td>";
    }
    echo "<td><a href = 'edit.php'>Edit</a> <a href = 'delete.php' onClick='confirmDelete()';>Delete</a></td>";
    echo "</tr>";
}

echo "</table>";
echo "<input type = 'button' name= 'add' value='ADD'>";

?>
</form>
</body>
</html>

and here is my delete.php code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="test1.php" name="delete">
<?php
echo "Delete";
?>
</form>
</body>
</html>

I don’t know whats wrong why the pop up message did not appear.

thank you

As far as I know javascript code shoud be written in lower case. I suggest that you check the error console to find what’s wrong.

Several problems:

Javascript is case sensitive, so be careful that you type your code according to the language specification.

the function, if, else, confirm, return, true and false keywords are all lowcase.


<script type="text/JavaScript"> 
function confirmDelete(){
   var agree = confirm("Are you sure you want to delete this file?");
   if (agree)
      return false ;
   else
      return false ;
   }
</script>

In your html you can’t have whitespace in your attribute declarations:

[COLOR=“Red”]

<a href = 'edit.php'>Edit</a> <a href = 'delete.php' onClick='confirmDelete()';>Delete</a>

[/COLOR]

it must be: attributename=“” rather than attributename = ‘’

Also, use double quotes (") to wrap the attribute values rather that single quotes (').


<a href="edit.php">Edit</a> 
<a href="delete.php" onClick="confirmDelete();">Delete</a>

This works:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/JavaScript">
function confirmDelete(){
	var agree = confirm("Are you sure you want to delete this file?");
	if(agree){
		// direct browser to delete.php
		window.location = "./delete.php";
	} else {
		// do nothing and return false
		return false ;
	}
}
</script>
</head>
</head>
<body>
<a href="edit.php">Edit</a><a href="#" onClick="confirmDelete();">Delete</a>
</body>
</html>

The single quotes dont matter so much, with it being inside php it makes life easier to single quote them. Semantically incorrect but not to worry too much!

I like zbing’s way of doing the window.location to redirect, the alternative which you should know about for future reference would be"


<a href='delete.php' onClick='return confirmDelete()';>Delete</a>

By return’ing the Javascript functions response, it stops the link being followed no matter what gets chosen. If you dont return the response then it makes no difference what the user clicks, the link will still go through.