Forum Moderators: open

Message Too Old, No Replies

Prototype form submit

         

tina001

7:38 pm on Jul 18, 2009 (gmt 0)

10+ Year Member



Hi,

I am trying to do a simple form checker using prototype.

I want, on submit, the user to be asked "are you sure.. y/n? If yes, submit the form, if no, then don't.

I also have a select box in the form and want to pass along the selected values id as a parameter.

Basically, the form is:


<form name="mform" method="post">
<select name="select">
<option name="option1">Option1</option>
<option name="option2">Option2</option>
</select>
</form>

I have something like this so far but I'm nowhere close to what I need:


<script "text/javascript">
Event.observe(window, 'load', function() {
Event.observe('mform', 'submit', function()
{
alert('test')});
});
</script>

Does anybody know how to get this to work?

Thanks in advance :)

Little_G

11:05 pm on Jul 18, 2009 (gmt 0)

10+ Year Member



Hi,

Small mistake:

<script [b]type=[/b]"text/javascript">

And javascript needs id's instead of names:
<form [b]id="mform"[/b] name="mform" method="post"> 

To give the user the option to abort the form submission you can use a confirm dialog and check the return value:


Event.observe("mform", "submit", function(event){
var response = confirm("Are you sure you want to submit this form?");
if(!response){
event.stop();
}
});

You can access the select box's value with

$('[i]select box id here[/i]').value

Andrew

tina001

12:25 am on Jul 19, 2009 (gmt 0)

10+ Year Member



Thanks you Andrew. Using your example above, it does nothing and the error console in FF tells me:

element is null

Do you know what the problem might be? I guess it doesn't recognize mform?

Little_G

12:44 am on Jul 19, 2009 (gmt 0)

10+ Year Member



Which statement is throwing that error?

Andrew

tina001

12:58 am on Jul 19, 2009 (gmt 0)

10+ Year Member



I'm not sure, it's just exactly like I posted above.

I didn't add in the select box code yet, just the above.

When I add back in the:


Event.observe(window, 'load', function() {

There is no error, but it doesn't trigger the event. This i what I am using:

<script type="text/javascript">
Event.observe(window, 'load', function() {
Event.observe("mform", "submit", function(event){
var response = confirm("Are you sure you want to delete?");
if(!response){
event.stop();
}
})
});
</script>

Little_G

1:09 am on Jul 19, 2009 (gmt 0)

10+ Year Member



Are you still adding the submit listener inside a window load listener?

Andrew

tina001

1:17 am on Jul 19, 2009 (gmt 0)

10+ Year Member



Yes, exactly like I posted above, this gives no error but does nothing. But when I take out the window listener and just leave it exactly like you posted above, I get the "element is null" error and no more information from the error.

Little_G

1:22 am on Jul 19, 2009 (gmt 0)

10+ Year Member



Have you added a submit button to your form?

Andrew

tina001

1:30 am on Jul 19, 2009 (gmt 0)

10+ Year Member



I have:


<input type="submit" name="submit" id="submit" value="Delete" />

Little_G

10:14 am on Jul 19, 2009 (gmt 0)

10+ Year Member



Hi,

Can you post the whole page please?

Andrew

whoisgregg

5:15 pm on Jul 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This event listener for the form:

 Event.observe("mform", ...

Depends on the <form> tag having the ID "mform" assigned, like so:

<form id="mform"