Solved

JavaScript confirm msgbox delete records from MySQL database

Posted on 2008-06-18
6
7,648 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now