Jump to content
php.lv forumi

jquery doubleposting


false

Recommended Posts

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,"_");

post-4101-0-74783200-1351939235_thumb.jpg

Edited by false
Link to comment
Share on other sites

$(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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...