1

I want to accomplish the following using jQuery:

frm_rooms.action="create_rooms.php?action=Save";
frm_rooms.submit();

I tried following jQuery but it doesn't work:

$('#frm_rooms').submit(function(event) <br/>{
    $.post("create_rooms.php", { action: "Save" } );
});
1
  • Hi all; thanks for suggestions & help :) .I got the solution from one of online guy thanks to him :) $("#frm_rooms").attr("action", "create_rooms.php?action=Save") $('#frm_rooms').submit(); Worked Fine :) Commented Jul 4, 2011 at 8:15

5 Answers 5

3

Do it like this:

$('#frm_rooms').submit(function(event){
  $.post("create_rooms.php", {action: "Save" }, function(){ 
     alert("Data saved");
  });
  return false; //This is vital
});

If you want the parameters to be passed in the query string (GET method) use $.get instead of $.post.

EDIT: Thinking better, if you have fields inside your form that you want to be submitted, you should do:

$('#frm_rooms').submit(function(event){
  $.post("create_rooms.php?action=Save", $(this).serialize(), function(){ 
     alert("Data saved");
  });
  return false; //This is vital
});

Hope this helps. Cheers

Sign up to request clarification or add additional context in comments.

5 Comments

As far as I remember, return false won't work on IE, it will still re-submit the form.
@Sayem No. It works cross browser. Return false is equivalent to event.preventDefault() plus event.stopPropagation()
Edgar thanks.. tried the script which you suggested. the form still doesnt get submitted. :(
@Sayem Thanks to you @arag Did you try using $.get? did you try the 2 ways I post? Does your form have elements to submit?
@ Edgar; thanks, yeah i have tried them. i didnt want ajax way. i just wanted form to be submitted normally with variable action value set to save ; just as this javascript " frm_rooms.action="create_rooms.php?action=Save"; " does; I got the solution; 1 of other guy suggested ; i have posted the thing just below my question
0

Have you tried to use preventDefault() in your submit-function?

$('#frm_rooms').submit(function(event)
{
    event.preventDefault();
    $.post("create_rooms.php", { action: "Save" } );
});

Also be aware that if you also have an input-element within your form, which name is 'submit'. The submit-method of jQuery won't work.

1 Comment

yeah had tried that event.preventDefault(); doesnt work. only $('#frm_rooms').submit() works fine. there is no input element with submit name/id
0

Add return false; after $.post() to avoiding page reloading.

1 Comment

page doesn't reload . i want the page to get submitted as normal along with value of action attached
0

Try this -

$('#frm_rooms').submit(function(event)
{
    $.post("create_rooms.php", {action: "Save" } );
    event.preventDefault();
});

If you have an input element of type submit inside that form, then this method won't work. In that case, you will have to do something like following -

$('#mySubmitButton').click(function(event)                 // Suppose the id of that 
                                                           // submit button is mySubmitButton
{
    $.post("create_rooms.php", {action: "Save" } );
    event.preventDefault();
});

If you want to provide success/failure message to the user, then modify the $.post method like this -

$.post("create_rooms.php", {action: "Save" }, function(data)
{
    // data contains server response. 
});

1 Comment

thanks...have tried it sayem.. its same thing happening. form not getting submitted
0

You can add .click() method to your button:

$('#button').click(function() {
   //...
   $('#form').attr('action', 'action.php');
   return false;
})

Comments

Your Answer

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.