JavaScript 'Are you sure?' type window prompt

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

JavaScript 'Are you sure?' type window prompt

Post by jayshields »

Hi guys, another question...

I'm currently coding an application where you can press the delete button and the record will be deleted, but I want some sort of 'Are you sure?' window to appear with OK and Cancel buttons, cancel stops the action, and OK lets it continue. I know I need some JavaScript here, but even with it, would I be able to do something simlar to this...

Code: Select all

if user wants to delete a record then
  prompt them with an are you sure window

  if the user clicked ok in the are you sure window then
    delete the record
  end if
end if
I'm presuming it's not that simple, and I'm going to have to send some sort of HTTP request using AJAX or whatever.

An example of what I am trying to achieve can be seen by deleting something in PHPMyAdmin.

Can someone tell me how this kind of thing would be done?

Ps. I'm lacking JavaScript experience :)

Thanks.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

It's quite trivial. Take a look at the onsubmit handler of the following form:

Code: Select all

<form action='deleterecord.php' method='POST' onsubmit='return confirm("Are you sure?");'>
 <input type='hidden' name='id' value='123' />
 <input type='submit' name='delete' value='Delete record' />
</form>
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

By "It's quite trivial", I thought you meant it was going to be very difficult... :lol:

The onSubmit you provided me with works great.

Thanks alot!
Post Reply