Page 1 of 1

JavaScript 'Are you sure?' type window prompt

Posted: Tue Jan 31, 2006 10:40 am
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.

Posted: Tue Jan 31, 2006 10:55 am
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>

Posted: Tue Jan 31, 2006 3:43 pm
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!