Solved

JavaScript confirm msgbox delete records from MySQL database

Posted on 2008-06-18
6
7,680 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 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