Jump to content

javascript & php confirmation


son.of.the.morning

Recommended Posts

try this it may work i have no idea

<html>
<head>
<script type="text/javascript">
function confirmation() {
var answer = confirm("Delete Record??")
if (answer){
	<?php *put delete code here* ?>
}
else{
	alert("Did not delete")
}
}

</script>
</head>
<body>
<form>
<input type="button" onclick="confirmation()" value="delete record">
</form>
</body>
</html>

Link to comment
Share on other sites

Assuming it just associated with a button ... you could do something like this ...

 

<script type = 'text/javascript'>
    function checkDelete ( id )
    {
         if ( confirm ( "Are you sure you want to delete " + id + "?" ) )
         {
              // relocate to another page
              document.location.href = 'deleteurl.php?id=' + id;
         }
         else
         {
               alert ( "Stop the presses!" );
         }
     }
</script>

 

Then in your actual page you would have something like:

<input type = 'button' onclick = 'checkDelete("<?php echo $id; ?>")' />

 

~juddster

Link to comment
Share on other sites

PHP is run on the server, before the page is sent to the user. Javascript is run in the browser. You cannot mix the two.  Your PHP can write Javascript to be "added" to the page, but it cannot execute Javascript code. Javascript can issue a request to the server (AJAX) to execute a PHP script on the server and collect the results, but it cannot call PHP code directly.

 

One way to accomplish your goal would be to use an OnSubmit handler for your form. As an example, I will take the code suggested by Rifts and make a few changes

<html>
<head>
<script type="text/javascript">
function confirmation() {
var answer = confirm("Delete Record??")
if (answer){
	return true;
} else {
	return false;
}
}
</script>
</head>
<body>
<form action="/doDelete.php" method="POST" onSubmit="return confirmation()">
<input type="button" value="delete record">
</form>
</body>
</html>

 

The form has to send a new request to the server to do the deletion.  This will be a new script you will have to write. I've named it "doDelete.php", you can call it "fred.php" or "wilma.php" or whatever you want.  But that script will have to do the delete.

 

The "onSubmit" attribute of the FORM tag causes the specified function to be executed.  This attribute has a "return" command in it, so that whatever the function returns is returned to the form handler (seems redundant to me too, but I didn't write the spec, and the didn't ask me). If the function "confirmation()" returns false, (and we return false to the submit handler) then the default action will not be performed. If we return true, then the default action will be performed. The default action is to send the form data to the script we specified in the action attribute of the form tag.

 

The doDelete.php script will need to send a new page to the user. You can either show a confirmation, or redirect to the original page, or tell them good bye, whatever is appropriate.

 

This functionality could also be accomplished using AJAX without having to refresh the page. But that will be more complicated.

Link to comment
Share on other sites

You can pass it as a hidden field on the form:

<form action="/doDelete.php" method="POST" onSubmit="return confirmation()">
<input type="button" value="delete record">
<input type="hidden" name="delFile" value="filename.ext">
</form>

Then retrieve the value from $_POST['delFile'] (or whatever name is assigned to the hidden field).

Link to comment
Share on other sites

I found a simple approach to this without using JavaScript...

I passed the id to another page with has an image resembling a confirmation box (obviously this is a nice custom box) the image has to hotspots one being yes one being no. When yes is to be clicked i pass the var to the delete-image.php page, no will obviously return back to the table page.

 

Link to comment
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.