1

Could someone provide me with the most simple code for form submission with jquery. On the web is with all sorts of gizmo coding.

2
  • $('form').submit() is an obvious answer - but probably not what you are searching for. Can you be a bit more descriptive what do you want to do? Commented Jan 11, 2010 at 10:24
  • I have a simple delete record page, and I want to delete a record. I dont want any refresh, and require jQuery for the work Commented Jan 11, 2010 at 10:29

4 Answers 4

2
$('#your_form_id').submit(function(){
  var dataString = $("#your_form_id").serialize();

  $.ajax({  
    type: "POST",  
    url: "submit.php",  
    data: dataString,  
    success: function() {  
      alert('Sent!');
    }  
  });

  return false;
});
Sign up to request clarification or add additional context in comments.

3 Comments

right, I want to return a value from the submit.php once the sql query is executed. like I am adding a comment to your answer
@dave: See this question on how to do that: stackoverflow.com/questions/388436/jquery-ajax-return-value
The success function is given the parameters (data, textStatus). You can behave according to the received data in the success fucnction.
1

here is a another solution, not as simple as the Jquery Form Plugin, but it can be useful if you want to handle errors codes and messages by yourself

look at this HTML + Javascript sample :

    <div>
      <form method="post" id="fm-form" action ="">
      <label>Name:</label>
          <input type="text" id="fm-name" name="fm-name" value="" />
          <label>Email:</label>
          <input type="text" id="fm-email" name="fm-email" value="" />
          <label>Birthdate:</label>
          <input type="text" id="fm-birthdate" name="fm-birthdate" value="" />

          <input type="submit" id="fm-submit" value="Save it">
      </form>
    </div>


<script type="text/javascript">
$(function() {

  // disable the form submission
  $("#fm-form").submit(function () { return false; });

  // post the datas to "submit_form.php"
  $("#fm-submit").click(function() {
  $.post("/ajax/submit_form.php",
      { 'fm-name':$("#fm-name").val(),
      'fm-email':$("#fm-email").val(),
      'fm-birthdate':$("#fm-birthdate").val()
      }
      ,function(xml) {
        // submit_form.php will return an XML or JSON document
      // check it for potential errors
      });
  });

});
</script>

Comments

0

What you want is jquery form plugin. It allows you to simply send normal 'form' using ajax - you can make a non-visible form and use this plugin to subnmit it. The option in Joel's answer is possible as well, it depends on the complexity of the thing you want to submit.

2 Comments

true, I will be adding stuffs. Does it work out to be the same as adding a comment to your answer?
Yes, it does - I am not sure if stackoverflow uses the form plugin in particular though :)
0

Take a look at the Form plugin:

$(function() {
    $('#myForm').ajaxForm(function() { 
        alert("Thank you for your comment!"); 
    }); 
});

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.