Solved

JavaScript confirm msgbox delete records from MySQL database

Posted on 2008-06-18
6
7,688 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 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