1

I am trying to submit various forms on the same page via jQuery/AJAX, it is for an admin site where you edit users so the url/action for the form gets generated dynamically. e.g

/user/limit/update?userId=7

or

/user/address/update?id=2.

is there a way find the forms url/action, store it in a variable and pass it the the url in the ajax function?

hopefully the example will make it more obvious.

the generated form tag is

<form action="/user/update?id=3" method="post" accept-charset="UTF-8" enctype="application/x-www-form-urlen" id="idfff">

and updated ajax from the suggestion is

$("#user-submit").click(function() { 
 $.ajax({ 
 type: "post", 
 url: $('#idfff').attr('action'),
 data:$('#user-update').serialize(),
 success: function() {
 $("<div class='update-success'>Details updated</div>").insertAfter("#user-submit");
 }
 });
 return false; 
});

Hopefully I have explained my problem correctly but please let me know if you require further info.

Thanks

asked Jul 24, 2012 at 11:34
4
  • The handler is generic enough to bind it to all form submits with a class instead of having one handler per form bond to the id. You'd need to change a few things like $('#limits').serialize(), to $(this).parents('form').serialize(), Commented Jul 24, 2012 at 11:43
  • It does the same thing does it not? I'm not fussed about the smaller points at the moment just trying to get the submit to work then I can tidy. Commented Jul 24, 2012 at 12:01
  • It actually does. Didn't mean to upset your plan, it was just a suggestion ;) Commented Jul 24, 2012 at 13:53
  • Oh I wasn't belittling your contribution it was just a test on 1 form run so I wasn't worried about the details. Commented Jul 24, 2012 at 14:04

2 Answers 2

2

Use attr() function on the form element to get action attribute value. In your example it should be something like this: url: $('#limits').attr('action')

answered Jul 24, 2012 at 11:36
Sign up to request clarification or add additional context in comments.

4 Comments

seems so obvious (slaps forehead), as I finished writing the question I kind of new what the answer would be, for some reason I was asking the back end developer to create a play variable and just blanked on the obvious answer. I will test it out and accept when I can. thanks
it still posts via page refresh and not ajax, I have edited my question to include more info
You need to call preventDefault() to cancel normal post back, check this article
Thanks for your help, turns out the back end developer added another submit button with the same id, after I added your original suggestion it still wasn't working but got it now...
0

Something like the following?

$("#yourForm").attr("action");
answered Jul 24, 2012 at 11:38

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.