Jump to content

Recommended Posts

I'm trying to have a javascript confirm box pop up and confirm the intention to delete a record in a database. I think I'm having trouble calling the function within my PHP code, but not sure what exactly is wrong. The status bar shows something like "javascript:checkDelete(100, Title)" when I hover over the link and that seems right but nothing occurs when I click on it. Here is my code:

 

Javascript

<script language="Javascript">

function checkDelete(b,Title){
	var del=confirm("Are you sure you want to delete " + Title + " from the Database?");
	if (del){
			document.location="delete.php?mID=" + b + "&Title=" + Title;
	}
}

</script>

 

PHP

 echo "<td><a href='Javascript:checkDelete(" . $row['mID'] . ","  . $row['Title']  . ")'>";
echo "<img border=0 src='images/delete.gif'>" . "</a></td></tr>";

 

Anyone see my error?

 

 

Link to comment
https://forums.phpfreaks.com/topic/218908-javascriptphp-help/
Share on other sites

No need for a function here, unless you just want it.

echo "<td><a href=\"delete.php?mID={$row['mID']}&Title={$row['Title']}\" onclick=\"return confirm('Are you sure you want to delete {$row['Title']} from the Database?')\">";
echo "<img border=0 src='images/delete.gif'>" . "</a></td></tr>";

Link to comment
https://forums.phpfreaks.com/topic/218908-javascriptphp-help/#findComment-1135286
Share on other sites

The function wouldn't work because you double nested it with single quotes.

 

<a href='Javascript:checkDelete(234,'Title');'>

 

Will not work as the single quote in the javascript function would close the href in the html element.

 

To fix this, you would have to write your php echo statement with double quotes.

 

echo "<td><a href=\"Javascript:checkDelete(" . $row['mID'] . ",'"  . $row['Title']  . "')\">";

Link to comment
https://forums.phpfreaks.com/topic/218908-javascriptphp-help/#findComment-1135317
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.