false Posted November 3, 2012 Report Posted November 3, 2012 (edited) Kāpēc uzspiežot "Create new topic" pogu, jquery izsūta divus pieprasījumus? Un datubāzē parādas 2 vienādi ieraksti? Ir kādas iespējas šo problēmu noverst? javascript $(document).ready(function(){ $('#topicForm').submit(function(e) { submitNewTopic(); e.preventDefault(); }); }); function submitNewTopic(){ hideshow('loading',1); error(0); $.ajax({ type: "POST", url: "/submitnewTOPIC.php", data: $('#topicForm').serialize(), dataType: "json", success: function(msg){ if(parseInt(msg.status)==1){ $('#postT').html("Completed"); }else if(parseInt(msg.status)==0){ error(1,msg.txt); } hideshow('loading',0); } }); } function hideshow(el,act){ if(act) $('#'+el).css('visibility','visible'); else $('#'+el).css('visibility','hidden'); } function error(act,txt){ hideshow('error',act); if(txt) $('#error').html(txt); } html <div id="postT"> <form id="topicForm" method="post"> <textarea name="txt" id="txt" style="width:430px; height:150px"></textarea><br/> <input type="submit" value="Create new topic"> </form> </div> submitnewTOPIC.php function msg($status,$txt){ return '{"status":'.$status.',"txt":"'.$txt.'"}'; } if(empty($_POST['txt'])){ die(msg(0,"You must complete all fields!")); } $createtopic = $DBH->prepare("INSERT INTO topics(`author`, `text`, `when`) VALUES(:author, :text, :when)"); $createtopic->execute(array(":author"=>$userID, ":text"=>$_POST['txt'], ":when"=>time())); echo msg(1,"_"); Edited November 3, 2012 by false Quote
briedis Posted November 3, 2012 Report Posted November 3, 2012 A nafig tev tur ajax vispār, ja ir forma? Divi ieraksti ir tāpēc, ka izpildās ajax, kā arī tiek nosubmitota vienkārši forma. Quote
daGrevis Posted November 4, 2012 Report Posted November 4, 2012 Nop. Tiek izsaukta `preventDefault()` metode. Quote
indoom Posted November 5, 2012 Report Posted November 5, 2012 Tas javascript kods netiek ievietots lapā divās vietās? Quote
Džei Posted November 6, 2012 Report Posted November 6, 2012 $(document).ready(function(){ $('#topicForm').submit(function(){ hideshow('loading',1); error(0); $.post( '/submitnewTOPIC.php', { data: $('#topicForm').serialize() },function( msg ){ if( parseInt( msg.status ) == 1 ) { $('#postT').html("Completed"); } else if( parseInt( msg.status ) == 0 ) { error(1,msg.txt); } hideshow('loading',0); }, 'json'); return false; }); }); tas pats tikai mazliet savādāk, vajadzētu būt OK Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.