tags:

views:

20

answers:

2

I'd like to modify the way some of my edit forms work...on submitting the form, the user is prompted with an aler box. If he/she chooses "Yes" then the record will be edited...on choosing "Cancel" however, the record will be saved as a new record. Is this possible?

A: 

No — since alert boxes only have one button :)

You could do this with a confirm, doing something like:

myForm.onsubmit = function () {
    if (confirm('Foo?')) { 
        this.elements.record.value = "edit";
    }
}

and

<input type="hidden" name="record" value="new">

However, this violates rule one and you would be better off just having:

<input type="radio" name="record" value="new" id="record_new">
<label for="record_new">New record/label>
<br>
<input type="radio" name="record" value="edit" id="record_edit" checked>
<label for="record_edit">Edit existing record/label>
David Dorward
+1  A: 

I suppose your "alert box", which allows your user to choose between "Yes / No", is actually using the confirm() function ?

If so, confirm() will return a truthy or falsy value, depending on what the user choosed.


Which means you could use something like this :

if (confirm("Do you want to save ?")) {
    // save
} else {
    // do not save
}
Pascal MARTIN