-1

I have read just about every post on the subject that I can find here and I'm just not seeing what I'm doing wrong.

CODE:

<?php 

// This is a test of a confirm dialog. 

if(isset($_POST['delete'])){
    echo "Delete something";
} 

?>

<html>
<head>
    <title>Confirm Test</title>
</head>
<body>

    <form method="post">
        <input type="submit" name="delete" value="Delete" onclick="return confirm('Are you sure you want to do delete item?')">
    </form>

</body>
</html>

Now this simple test works but in my page it doesn't. Strange as it works else where in the site and the structure of the page doesn't appear to be any different. When the input button is pressed no confirm dialog appears and the page just reloads.

The page where it is not working is more complicated of course then what is above but it is basically the same. The code above for the most part is cut from it. If I cut and paste the test code into the body of the page it no longer works as it does in the test. No dialog is ever presented.

Hard question to answer I know but a point in the right direction to know what I can test and look for would be great.

Paul
  • 328
  • 1
  • 5
  • 17
  • It’s also a good idea _not_ to use inline JavaScript. Instead, abstract it out to a function you can apply to _any_ element. – Martin Bean Aug 28 '15 at 14:07
  • ...such as http://stackoverflow.com/a/18193924/1094772 – rybo111 Aug 28 '15 at 14:08
  • I tried that as well and no luck, Just doesn't call it. – Paul Aug 28 '15 at 14:22
  • its working https://jsfiddle.net/nhe1b43w/ – Farhan Aug 28 '15 at 15:25
  • Well I have not discovered that none of delete buttons around the site are calling the onclick handler any more. This isn't on more them one system as well. What could take place that would result in onclick handlers not being called? – Paul Aug 28 '15 at 15:56

1 Answers1

1

You are using onclick event of js which won't affect the form submission. You need to stop the form submission using onsubmit event.

Edited

<script>
  function validate(){
 return confirm('Are you sure?');
} 
</script>

Add it to form as:

 <form method="post" action="file.php" onsubmit="return validate();" >
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • Typo on my part, it works but I have two buttons not just one. And this is being called on both. Is there a way around that? – Paul Aug 28 '15 at 14:48
  • I keep trying work arounds to this but they require that I use the onclick event to set a variable and it just isn't being fired. – Paul Aug 28 '15 at 15:07
  • Try what now @DanyalSandeelo ? I love how people vote down a question that they don't have a valid answer for. Some of the onclick events work on the site but there are several places where it isn't working. I don't get what the difference is from one to the other. On submit works just fine, but I have two buttons and only want one to call the function before continuing. All solutions around that problem require that the onclick works, but it is not being called. – Paul Aug 29 '15 at 21:07
  • I modified my script..check now @PaulNeale – Danyal Sandeelo Aug 29 '15 at 21:37
  • Thanks, how does it solve the problem of two buttons @DanyalSandeelo? – Paul Aug 29 '15 at 22:47
  • which two buttons? this script is working now right? – Danyal Sandeelo Aug 30 '15 at 19:58
  • As I have mentioned. I have two buttons in the form and only one of them should request a confirm dialog. Thanks for the help though @DanyalSandeelo. There has to be another problem that is causing this. – Paul Sep 01 '15 at 22:01