Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

jquery submit form

I am trying to submit a form inside a modal window using jquery.

the form tag is:

<form action="forms_SPedit.asp" method="post" class="form-horizontal" id="form2" name= "form2">

I am using this script but the problem is that the form submits on load of the page instead of when I click the 'submit' button

<button type="submit" class="btn btn-primary" >Save changes</button>

and the script:

<script>
         $('#edit-modal').on('show.bs.modal', function(e) {
             
             var $modal = $(this),
                 esseyId = e.relatedTarget.id;
                          $modal.find('.edit-content').html(esseyId);
             
         })
             
                  
            $("form[name='form2']").submit()
</script>
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

That's because your .submit() is in the page load and not inside the event handler
// Not sure what show.bs.modal is but this does
// not look like a click handler

$('#edit-modal').on('show.bs.modal', function(e) {
	var $modal = $(this),
	esseyId = e.relatedTarget.id;
	$modal.find('.edit-content').html(esseyId);
})
// This appears to be in the onload
$("form[name='form2']").submit()

Open in new window


But now I am confused - you want to use jquery to submit the form when you press the submit button ... Why? Why not just let the submit button do what it is supposed to do?

To submit a form on an event you need to do something like this

$(function() {
  // ON LOAD FUNCTIONALITY HERE
 ...
   // SET UP EVENT HANDLER
    $('SOME_SELECTOR').click(function(e) {
      e.preventDefault();
      // INSIDE EVENT HANDLER 
      // NOT IN ON LOAD
      $('form[name="form2"]').submit();
  });
});

Open in new window

Avatar of Aleks

ASKER

Julian,

I am not familiar with jquery I am trying to figure things out still.

If I want the button to do the submit how would I change this code ?

--

$(function() {
  // ON LOAD FUNCTIONALITY HERE
 ...
   // SET UP EVENT HANDLER
    $('SOME_SELECTOR').click(function(e) {
      e.preventDefault();
      // INSIDE EVENT HANDLER
      // NOT IN ON LOAD
      $('form[name="form2"]').submit();
  });
});

--

button:

<button type="submit" class="btn btn-primary" >Save changes</button>

Do I need to add code to the button ?
Avatar of Aleks

ASKER

I tried the following:

Form:

<form action="forms_SPedit.asp" method="post" class="form-horizontal" id="form2" form="form2"></form>

Submit button:

<button type="submit" class="btn btn-primary" id = "mdlsave" >Save changes</button>

Script (from you)


<script>
         $('#edit-modal').on('show.bs.modal', function(e) {
             
             var $modal = $(this),
                 esseyId = e.relatedTarget.id;
                          $modal.find('.edit-content').html(esseyId);
             
         })
             
             $(function() {
  // ON LOAD FUNCTIONALITY HERE

   // SET UP EVENT HANDLER
    $('mdlsave').click(function(e) {
      e.preventDefault();
      // INSIDE EVENT HANDLER
      // NOT IN ON LOAD
      $('form[name="form2"]').submit();
  });
});
</script>

But still won't submit the form  :#
I am confused though - why do you need jquery to make a button submit? Form buttons by default submit the form - so why do you need JQuery to do it?
Avatar of Aleks

ASKER

I have a normal submit button. Above is the code.
Avatar of Aleks

ASKER

This is to submit the form:

<button type="submit" class="btn btn-primary" id = "mdlsave"  name = "mdlsave" >Save changes</button>

And this is the code you sent with the selector with the name 'mdlsave'

             $(function() {
   // ON LOAD FUNCTIONALITY HERE
  ...
    // SET UP EVENT HANDLER
     $('mdlsave').click(function(e) {
       e.preventDefault();
       // INSIDE EVENT HANDLER
       // NOT IN ON LOAD
       $('form[name="form2"]').submit();
   });
 });

But the form still not submitting. I tried without the code and just use a simple form and the submit button but for some reason it won't submit.
That is probably because of the errors in your markup.

You have two <form> declarations both of which terminate on the same line they are declared.

You need to fix those. Please refer to the sample
http://www.marcorpsa.com/ee/t962.html
To clarify
<form action="forms_SPedit.asp" method="post" class="form-horizontal" id="form2" form="form2"></form>

Open in new window


You need to put your controls between the <form> tags

<form action="forms_SPedit.asp" method="post" class="form-horizontal" id="form2" form="form2">
PUT YOUR FORM FIELDS HERE - NOT OUTSIDE
</form>

Open in new window

Avatar of Aleks

ASKER

Yes of course. Here is my whole form code:

--

 <form action="forms_SPedit.asp" method="post" class="form-horizontal" id="form2" form="form2"></form>
                    <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h3 class="modal-title">Edit form</h3><small>Please select the Beneficiary that will fill out the form and update the description.</small>
   
                </div>
                  <div class="panel-body">
                      <div >
                        <div class="ibox float-e-margins">
                          
                        <div class="form-group"><label class="col-sm-2 control-label">Beneficiary</label>
<div class="col-sm-10">
<select class="form-control m-b" name="account">
 <option>option 1</option>
 <option>option 2</option>
 <option>option 3</option>
 <option>option 4</option>
 </select>
 </div>  </div>
                          
                     
<div class="form-group"><label class="col-sm-2 control-label">Description</label>
<div class="col-sm-10"><input id = "idfield" name "idfield" type="text" class="form-control" placeholder="Mr. | Ms." value="<%=request("formcaseid")%>"></div>
</div>  
                    

                        
                        </div>
                      </div>
                    </div>
               
                <div class="modal-footer">
                      <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
                                   <button type="submit" class="btn btn-primary" id = "mdlsave"  name = "mdlsave" >Save changes</button>
                </div>
            </div>
            </form>

----
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

Seems to me there was an extra tag there because the form was closed at the bottom .. let me delete it and try ..
Avatar of Aleks

ASKER

Ok cool ! .. the form submits. I will close this ticket and concentrate on passing the value to the form.
Avatar of Aleks

ASKER

Thanks for the patience  :#
You are welcome.