Search code examples
javascriptphphyperlinkconfirm

How To Include Javascript Delete Confirm in a Link Using PHP?


This block of code builds a whole table with data in rows and 2 columns, Edit, and Delete. I figure someone might want that code besides me getting question answered. (by the way the implode function is taking 13 columns in my recordset and displaying them. Pretty concise.)

 $result = mysqli_query($conn, $sql); 


$tablebuild = "<table border = '1' >";
$tablebuild .="<tr><td>Edit</td><td>Delete</td>";
while ($property = mysqli_fetch_field($result)) {
   $tablebuild .= "<td>".$property->name."</td>";
}

$tablebuild .="</tr>";

while ($row = mysqli_fetch_row($result)) // Data
{ $tablebuild .= '<tr><td><a href="leader_edit.php?MemberID='.$row[0].'" onClick=\"return confirm("You sure?");\" >Edit</a></td><td><a href="leaders.php?delete=yes&MemberID='.$row[0].'">Delete</a></td><td align="center">'.implode($row,'</td><td align="center">')."</td></tr>\n"; }


$tablebuild .= "</table>";

echo $tablebuild;

?>

I put my onClick event in the link that calls Edit page for that MemberID because I didn't want to keep deleting Members while onClick wasn't working. After getting it to work I would then move it to nearly identical scripted link that calls delete function.

onClick=\"return confirm("You sure?");\"

However, it doesn't seem to be working. I've tried all sorts of escape and quotes but no luck. Maybe it isn't possible in a link. I think the code is a good concise way to display a recordset in Html table format and would be good to save in one's library of code.


Solution

  • Perhaps placing the message 'You sure?' in single quotes and escaping those will work for you:

    onClick="return confirm(\'You sure?\')"