Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JavaScript confirm msgbox delete records from MySQL database

Posted on 2008-06-18
6
Medium Priority
?
7,757 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 1000 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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…
Suggested Courses

636 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