javascript - $.post jQuery work only when alert inside post function -
my button :
<button type="submit" name="submit" class="btn-primary btn-block" id="submit">login</button>
and form :
<form action="b.php" method="post" id="formid" onsubmit="return post();">
and js function :
function post() { var cvs = $('#client-nbr').val(); var cs = $('#cs').val(); $.post('a.php',{postcvs:cvs,postch1:cs}); alert("post function work"); }
my problem is: when remove alert
post()
function, doesn't post a.php
, , when use return false
inside post()
function, function works on a.php
page freezes , can't data b.php
!
given want asynchronous $.post
complete before submitting form, need to:
- prevent submit executing immediately, returning false in
post
function - add success callback
$.post
call, in manually submit form. sure not trigger submit via jquery (using.trigger('submit')
or.submit()
on jquery object). cause infinite loop, triggerpost
function again. instead, useget(0)
raw dom element , use submit form, seen below.
try this:
function post() { var cvs = $('#client-nbr').val(); var cs = $('#cs').val(); $.post('a.php',{postcvs:cvs,postch1:cs}, function() { //manually submit form $('#formid').get(0).submit() }); return false; }
for more information adding callbacks $.post
, see https://api.jquery.com/jquery.post/
Comments
Post a Comment