Jquery and ajax for submitting form doesnt work -
this code for submitting form: aj.js $(document).ready(function(){
$("#reqform").submit(function(e){ e.preventdefault(); var f = e.target, formdata = new formdata(f), xhr = new xmlhttprequest(); xhr.open("post", f.action,true); xhr.send(formdata); xhr.onreadystatechange=function() { if (xhr.readystate==4 && xhr.status==200) { $("#alert").html(xhr.responsetext); $('#alert').css('visibility','visible').fadeout(5000); } } }); });
at first time when submit form, things , mycode run well. when want submit second, third ,.. time ajax code done , form data save in database, part of code doesnt work
xhr.onreadystatechange=function() { if (xhr.readystate==4 && xhr.status==200) { $("#alert").html(xhr.responsetext); $('#alert').css('visibility','visible').fadeout(5000); }
my insert file:
$result=mysqli_query($dbcnn,"insert request (name,telnum,email,request,date) values('$_post[name]',$_post[tel],'$_post[email]','$_post[request]',now())"); if(mysqli_affected_rows($dbcnn)<1) $_session['alert']="success!!"; else $_session['alert']="not!"; echo $_session['alert']; }
?>
it clear xhr.onreadystatechange
not firing after first time.
i not sure if solution. way registered form submission have impact.
so try linking submit event root document bubble every time submit , reload , this
$(document).ready(function(){ $(document).on("submit","#reqform",function(e){ e.preventdefault(); var f = e.target, formdata = new formdata(f), xhr = new xmlhttprequest(); xhr.open("post", f.action,true); xhr.send(formdata); xhr.onreadystatechange=function() { if (xhr.readystate==4 && xhr.status==200) { $("#alert").html(xhr.responsetext); $('#alert').css('visibility','visible').fadeout(5000); } } }); });
i hope work out
answer inspired : submitting form jquery/ajax works every other time
========== edit:
there thoughts on answers order matter, first open()
ajax request set event xhr.onreadystatechange=function()
use send()
if read true should work
try , let me know
Comments
Post a Comment