Solved

JavaScript confirm msgbox delete records from MySQL database

Posted on 2008-06-18
6
7,664 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:john-formby
  • 3
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 250 total points
ID: 21817271
You have misunderstood the way PHP operates in conjunction with HTML and Javascript.

PHP is a server-side language, which means all of the code is processed on the server. This is done before any output (HTML and Javascript) is sent to the user's browser. Therefore, in your script, the PHP portion is processed first, deleting the record, and then the Javascript and HTML is passed on to the user.

Your script is expecting that the code is processed in the following order:

 Some Javascript
 Some PHP
 Some more Javascript
 Some HTML

This is not correct.

You will need to alter your document to send the required HTML and Javascript, receive the user's answer, and then call another script with the request to delete the record.
0
 
LVL 36

Assisted Solution

by:Loganathan Natarajan
Loganathan Natarajan earned 250 total points
ID: 21819954
better have client java script confirm delete function.. .upon click .. call server side page to do the deletion ..,

look at this example and modify your code,
http://www.dmxzone.com/ShowDetail.asp?NewsId=3459
0
 
LVL 14

Author Comment

by:john-formby
ID: 21824822
Hi,

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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 24

Expert Comment

by:glcummins
ID: 21824844
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.
0
 
LVL 14

Author Comment

by:john-formby
ID: 21824884
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.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 21824889
Perfect, sounds good. Thanks for the points!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now