john-formby
asked on
JavaScript confirm msgbox delete records from MySQL database
Hi,
I have implemented a JavaScript function that should allow the user to confirm if they want to delete a record or not. This is the code I have:
if(isset($_POST['delete']) ) { ?>
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Are you sure you want to delete?")
if (answer){
<?php
mysql_query("DELETE FROM tblgbs WHERE gbID = $gbID") or die(mysql_error());
mysql_query("DELETE FROM tblgb WHERE gbID = $gbID") or die(mysql_error());
$success_msg = '<div class="errormsg">Deleted</ div>';
?>
}
else{
<?php
$success_msg = '<div class="errormsg">Not Deleted</div>';
?>
}
}
//-->
</script>
The problem is that the first time I enter the delete page, I do not get the confirm box, it just deletes the record. The second problem I have is that even if I select Cancel, it still deletes the record.
I am using the following submit button to trigger the function:
<input class="subbtn" type="submit" name="delete" value="Delete" onclick="confirmation()" />
Please can someone have a look at this and tell me where I am going wrong.
Many thanks,
John
I have implemented a JavaScript function that should allow the user to confirm if they want to delete a record or not. This is the code I have:
if(isset($_POST['delete'])
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Are you sure you want to delete?")
if (answer){
<?php
mysql_query("DELETE FROM tblgbs WHERE gbID = $gbID") or die(mysql_error());
mysql_query("DELETE FROM tblgb WHERE gbID = $gbID") or die(mysql_error());
$success_msg = '<div class="errormsg">Deleted</
?>
}
else{
<?php
$success_msg = '<div class="errormsg">Not Deleted</div>';
?>
}
}
//-->
</script>
The problem is that the first time I enter the delete page, I do not get the confirm box, it just deletes the record. The second problem I have is that even if I select Cancel, it still deletes the record.
I am using the following submit button to trigger the function:
<input class="subbtn" type="submit" name="delete" value="Delete" onclick="confirmation()" />
Please can someone have a look at this and tell me where I am going wrong.
Many thanks,
John
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can certainly do that, but keep in mind that the PHP will not be executed after the Javascript. You will need to make a new request to the server to allow PHP to run an if... statement.
ASKER
Yes, I post the user Id number the first time, grab it and store in a hidden field. When the user selects either Yes or No, I post the variable again and carry out the action. It seems to be working really well.
Perfect, sounds good. Thanks for the points!
ASKER
Thanks for the comments. I have decided to do it using PHP by displaying an additional 2 submit buttons if user selects delete. Then using an If....Elseif statement to determine the course of action.
Thanks,
John