javascript - Html form submit button not calling succes method? -



javascript - Html form submit button not calling succes method? -

html form:

<form class="form-horizontal" id="contactfrm" onsubmit="contactus()"> <fieldset class="scheduler-border"> <!-- form name --> <legend class="scheduler-border">fill contact details</legend> <!-- text input--> <div class="form-group"> <label class="col-md-4 control-label" for="fname">first name</label> <div class="col-md-6"><input id="fname" name="firstname" type="text" placeholder="first name" class="form-control input-md" title="enter first name" required="required"></div> </div> <!-- text input--> <div class="form-group"> <label class="col-md-4 control-label" for="lname">last name</label> <div class="col-md-6"><input id="lname" name="firstname" type="text" placeholder="last name" class="form-control input-md" title="enter lastly name" required="required"></div> </div> <!-- button --> <div class="form-group"> <label class="col-md-4 control-label" for="button"></label> <div class="col-md-4"> <button type="submit" value="submit" class="btn btn-success" id="submit" /> </div> </div> </fieldset> </form>

script code:

<script type="text/javascript"> function contactus(){ alert("hi"); var test = json.stringify({ "help": $('#help').val(), "firstname": $('#fname').val(), "lastname":$('#lname').val() }); alert("test values are"+test); $.ajax({ type: "post", contenttype: 'application/json', url: baseurl+"contact/add", data: test, datatype:"text", success:successmethod, error: function(data,status) { alert("error "+status); } }); } function successmethod(data){ document.getelementbyid("contactfrm").reset(); $('#showcontactmessage').show(); alert("contact details saved"); } </script>

anchor tag:

<a href="javascript:contactus();" class="btn btn-success">contact now</a>

in above form when utilize anchor tag submit form every thing works fine ie; function called , values saved in database , success method called , executed.but problem here is, when utilize button tag submit form values, values saved in databse succes method not called instead calling error method..any help appreciated??

the submit handler not prevent form submitting.

the javascript run, form submit. browser leave current page , load new 1 before gets response ajax request. event handler won't exist on new page.

you need prevent default form action. using 90s style approach taking, need homecoming false event handler.

onsubmit="contactus(); homecoming false;"

javascript jquery html forms button

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -