简体   繁体   中英

Submitting form by jquery using method=post

i want to submit a form by jquery. i write the query form as follow

$("#form").submit(function(event) {
    if(!$("#change").hasClass("has-error") && !$("#name1").hasClass("has-error") 
            && !$("#contact1").hasClass("has-error") && !$("#batch1").hasClass("has-error")){
            alert("submitting");
            event.preventDefault();
            var $form=$(this);
            name1=$("#name").val();
            contact1=$("#contact").val();
            email1=$("#email").val();
            city1=$form.find("input[name='city']").val();
            company1=$form.find("input[name='company']").val();
            url=$form.attr("action");
            var posting= $.post("some_link.php",{ name:name1,contact:contact1,email:email1,city:city1,company:company1});
            posting.done(function(data){
                alert("form submitted successfully");
                )
            })
            $("#reset").click();
            return false;

            }
    else return false;

});

but this is posting form by GET method not by POST method and also url form is redirected to other php page instead for remaining on this page.

here is how link look like http://www.example.com/current_page.php?name=jhgjk&contact=lksjf&email=lkdj%40ldkjf.clj&city=&company= but i want a normal post method as usually done by including method="post" in form

EDIT i got the mistake i was doing. i was missing a closing bracket for $.post and this was the reason the code was not working and i was banging my head.

I think you'd be much better using a technology called $.ajax :

$("#form").submit(function(e) {
    if(!$("#change").hasClass("has-error") && !$("#name1").hasClass("has-error") 
            && !$("#contact1").hasClass("has-error") && !$("#batch1").hasClass("has-error")){


            e.preventDefault();
            alert("submitting");

            $.ajax({ 
               url: 'some-link.php',
               type: 'POST',
               data: $(this).serialize(),
               success: function(data) {
                  alert('success!');
               },
               error: function(data) {
                  alert('error');
               }
            });


            }
    else return false;

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM