0

I have a html form on my webpage that contains an image that will trigger a dialog page when clicked. However, the form submission triggers and force a submit to occur. How can i prevent that from happening?

<script>
 //trigger dialog
 $('#submitIter').click(function(){
 $("#dialog-submit").dialog("open");
 return false
 });
 $("#dialog-submit").dialog({ 
 autoOpen: false,
 resizable: false,
 height: 200,
 width: 200,
 modal: true,
 buttons: {
 "Proceed": function(){
 //submit after user clicks proceed cannot seem to work
 document.forms[0].submit();
 $(this).dialog("close");
 }
 }
 });
</script>
<form action="{{ request.path }}" method="post" enctype="multipart/form-data">
 <input type="image" src="images/submit.png" id="submitIter">
</form>
asked Apr 7, 2011 at 14:30
3
  • Is the form being submitted when you're setting up your button? I.e., document.forms[0].submit() is actually executing when it gets to that line of code? Commented Apr 7, 2011 at 14:36
  • @Beez the condition for it to be submitted is when the user click proceed in the dialog Commented Apr 7, 2011 at 14:53
  • That became apparent when I read @EMMERICH's answer. And now it makes sense. Don't forget to select an answer! :) Commented Apr 7, 2011 at 14:55

4 Answers 4

2
<form onsubmit="return false">

This should do the trick.

answered Apr 7, 2011 at 14:32
Sign up to request clarification or add additional context in comments.

Comments

2
$('form').submit(function(){
 $("#dialog-submit").dialog("open");
 return false;
});

instead of your click handler, and change the input tag to a submit tag.

answered Apr 7, 2011 at 14:36

2 Comments

i cannot seem to trigger my document.forms[0].submit(); after adding a return false
See my answer. The .submit()'s return false will never allow your form to submit, so it has to be conditional.
1
$('#submitIter').click(function(e){
 e.preventDefault(); // prevents default button action
 $("#dialog-submit").dialog("open");
});
answered Apr 7, 2011 at 14:50

Comments

1
<script>
 //trigger dialog
 $('form').submit(function(){
 return $('form').data('confirmed') === true;
 });
 $('#submitIter').click(function(){
 $("#dialog-submit").dialog("open");
 return false;
 });
 $("#dialog-submit").dialog({
 autoOpen: false,
 resizable: false,
 height: 200,
 width: 200,
 modal: true,
 buttons: {
 "Proceed": function(){
 //submit after user clicks proceed cannot seem to work
 $('form').data('confirmed', true);
 $('form').submit();
 $(this).dialog("close");
 }
 }
 });
</script>

Basically, set a variable on the form's data object that it's ready to be actually submitted to the server in your proceed button. If the variable doesn't exist, yet, the submit function will return false which will allow your dialog to show up. If it has been confirmed by means of your proceed button, the form will submit.

I haven't tested that code, but if it doesn't work, hopefully you can understand the concept.

answered Apr 8, 2011 at 15:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.