MyBB Community Forums

Full Version: Can someone help with this AJAX script as i really need it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
The script is like the one here
Where u can edit post without refresh with a 'Save' and 'Cancel' Button but
It works ine except it doesnt send the NEW value to the php script

The code is

function WBGetID(IDName) {

   if(document.getElementById) 
   {
    return document.getElementById(IDName);
   }
   
   else if(document.all) 
   {
    return document.all[IDName];
   }
   
   else if(document.layers) 
   {
    return document.layers[IDName];
   }
   
   else 
   {
   return null;
   }
};

function WBQuickEditPost(GetIDValue,GetRedirectURL) {
  originalhtml=WBGetID(GetIDValue).innerHTML;
  cID=GetIDValue.replace(/post_message_/,"");
  originalID=GetIDValue;
  WBGetID(originalID).innerHTML='<img src="../pic/spinner-big.gif" border="0" class="inlineimg"> <strong>Please wait ...</strong>';
  GETRequest('action=edit_post&newsid='+cID,'ajax.php');
}

function WBQuickEditPostCancel() {
  WBGetID(originalID).innerHTML=originalhtml;
}

function createXMLHttpRequest() {
  xmlHttp=false;
  try {
  xmlHttp=new XMLHttpRequest();
}

catch(e) {
  var _ieModelos=new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
  for(var i=0;i<_ieModelos.length&&!success;i++) {
  try {
  xmlHttp=new ActiveXObject(_ieModelos[i]);
 }
catch(e) {
  }
 }
}
 
if(!xmlHttp) {
  alert("Cannot compute");
  return false;
 }
}

function GETRequest(query,filename) {
  createXMLHttpRequest();
  var queryString=filename+"?"+query;
  xmlHttp.onreadystatechange=handleStateChange;
  xmlHttp.open("GET",queryString,true);
  xmlHttp.send(null);
}

function POSTRequest(query,url) {
  createXMLHttpRequest();
  xmlHttp.open("POST",url,true);
  xmlHttp.onreadystatechange=handleStateChangeP;
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.send(query);
}

function handleStateChange() {
  if(xmlHttp.readyState==4) {
  if(xmlHttp.status==200) {
  parseResults();
  }
 }
}

function handleStateChangeP() {
  if(xmlHttp.readyState==4) {
  if(xmlHttp.status==200) {
  parseResults2();
 }
 }
}

function parseResults2() {
  result=xmlHttp.responseText;
  if(result.match(/<error>(.*)<\/error>/)) {
  message=result.match(/<error>(.*)<\/error>/);
  if(!message[1]) {
  message[1]="There was a problem with the request. Please report this to administrator.";
  }
  alert("There was an error.<br><br>"+message[1]);document.quick_edit_form.submit.value="Save";document.quick_edit_form.submit.disabled=false;
  }
  else {
  WBGetID(originalID).innerHTML=result;
 }
}

function parseResults()
{
result=xmlHttp.responseText;if(result.match(/<error>(.*)<\/error>/))
{
message=result.match(/<error>(.*)<\/error>/);
if(!message[1])
{
message[1]="There was a problem with the request. Please report this to administrator.";
}
alert("There was an error performing the update.<br><br>"+message[1]);WBQuickEditPostCancel();}
else
{
WBQuickForm='<form name="quick_edit_form" onsubmit="return false;"><textarea style="width: 100%; height: 150px;" name="newContent">'+result+'</textarea><br><input type="submit" class="btn2" name="submit" value="Save" onclick="WBQuickSavePost()"> <input type="button" class="btn2" value="Cancel" onclick="WBQuickEditPostCancel()"></form>';
oldDiv=WBGetID(originalID);
newDiv=document.createElement(oldDiv.tagName);
newDiv.id=oldDiv.id;
newDiv.className=oldDiv.className;
newDiv.innerHTML=WBQuickForm;
oldDiv.parentNode.replaceChild(newDiv,oldDiv);
}
}


function WBQuickSavePost() {
  var cbobject=document.quick_edit_form.newContent.value;
	if(cbobject == "" || cbobject == "NULL")
	{
		alert('You need to fill in message!');
		return;
	}
  document.quick_edit_form.submit.value="Please wait ...";
  document.quick_edit_form.submit.disabled=true;
  POSTRequest('action=edit_post_save&text='+encodeURIComponent(cbobject)+'&newsid='+cID,'xmlhttp.php');
}

The problem is probably here
POSTRequest('action=edit_post_save&text='+encodeURIComponent(cbobject)+'&newsid='+cID,'ajax.php');
Cuz when i press edit in the page where i want it to show it opens the textarea etc it works fine but when i press 'Save' it doesnt save anything at all
And i have checked the action and file everything is ok Sad
Please help!

P.S
Nor when i press the save button does it show the text that was editted
Please tell me are there any functions missing?
change this ajax.php to the file php file that updates the database.
That is the file
(2008-06-18, 09:28 PM)dikidera Wrote: [ -> ]That is the file

Your signature image is way to big. Have you read the MyBB rules?
Sorry [Image: icon_dontknow.gif]
is it a submit button that you have?
It works fine calling the textarea but when i click on save it does nothing Sad
it is in the code
Hope u can help me [Image: bowdown.gif]
try this:
<script type="text/javascript">
submitHandler: function() { header("Location: PAGE TO PATH OF PHP FILE THAT UPDATES THE DB"); } });
</script>
I dont understand where to add it
The problem is in the js
add it in the header of the file that calls the js file.
Pages: 1 2