Jump to content

[SOLVED] script works but still get error message


kcp4911

Recommended Posts

Hi. Very new to the AJAX scene. Have dabbled and have managed to make a couple of scripts work.

 

The following script does work - and the earlier version (before adding added deletion functionally as described below) worked without errors.

 

I am trying to make a twitter/status type "widget" that will update without reloading the page. Now, that part works without any problems.

 

However, I also wanted to add the ability for a poster to delete their post. This works too - but I also get the error message (alert("Error during AJAX call. Please try again")). Easy answer would be to remove the error message from the script - but I feel that might bite me on the bum later on.

 

Here is the main "widget" page...

 

<?php include 'security_check.php';?>
<html>
<head>
<script type="text/javascript">
var time_variable;

function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object

function ajaxFunction() {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var detail = document.getElementById("detail");
    xmlhttp.open("POST","update.php",true); //calling update.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("detail=" + detail.value); //Posting detail to PHP File
  }
}

function ajaxFunction2() {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var delete_this2 = document.getElementById("delete_this2");
    xmlhttp.open("POST","update2.php",true); //calling update2.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("delete_this=" + delete_this2.value); //Posting delete_this to PHP File
  }
}

function ajaxFunction3() {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var delete_this3 = document.getElementById("delete_this3");
    xmlhttp.open("POST","update2.php",true); //calling update2.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("delete_this=" + delete_this3.value); //Posting delete_this to PHP File
  }
}
function ajaxFunction4() {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var delete_this4 = document.getElementById("delete_this4");
    xmlhttp.open("POST","update2.php",true); //calling update2.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("delete_this=" + delete_this4.value); //Posting delete_this to PHP File
  }
}
function ajaxFunction5() {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var delete_this5 = document.getElementById("delete_this5");
    xmlhttp.open("POST","update2.php",true); //calling update2.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("delete_this=" + delete_this5.value); //Posting delete_this to PHP File
  }
} 
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element 
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}
</script>
<script type="text/javascript" src="characterCounter.js"></script>
</head>
<body>
<div id="message" name="message">
<?php
include 'connect.php';
$i = 2;
$query = "
SELECT 
*
FROM 
updates
order by id desc limit 4
";

$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());

if (mysql_num_rows($result)) 
while($row = mysql_fetch_assoc($result)) 
{
extract($row);
$detail = stripslashes("$detail");
$uid = stripslashes("$uid");
if($_SESSION['uid']==$uid)//if the logged in user is the author of the comment, provide the option to delete
{
?>
<p><form name="<?php echo $i; ?>"><input type="hidden" name="delete_this<?php echo $i; ?>" id="delete_this<?php echo $i; ?>" value="<?php echo $id; ?>"><?php echo $detail; ?> - <input type="image" src="images/delete.jpg" value="Submit" align="absmiddle" onclick="ajaxFunction<?php echo $i; ?>();" /></form></p>
<?php
}
else//if the logged in user is not the author of the comment, don't provide the option to delete
{
?>
<p><?php echo $detail; ?></p>
<?php
}
$i++;
}
?>
</div> 
<form name="myForm">
<p><textarea rows="2" cols="37" name="detail" id="detail" onKeyUp="toCount('detail','sBann','{CHAR} characters left',200);">Add your own "Personal Par". Your name and leaving year will be added automatically.</textarea></p>
<span id="sBann" class="minitext"> 200 characters left.    </span> <input type="button" value="Submit" onclick="ajaxFunction();" /> 
</form>
</body>
</html>

 

Here is the update2.php file which is used for deleting the post and returning the ammended set of posts

 

<?php
require 'security_check.php';
require 'connect.php';

$id = mysql_real_escape_string($_POST['delete_this']);
$fname = $_SESSION['first_name'];
$lname = $_SESSION['surname'];
$year = $_SESSION['year'];
$i = 2;

$query = "
delete from
updates
where
id = '$id'
";
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());


$query = "
SELECT 
*
FROM 
updates
order by id desc limit 4
";

$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());

if (mysql_num_rows($result)) 
while($row = mysql_fetch_assoc($result)) 
{
extract($row);
$detail = stripslashes("$detail");
$uid = stripslashes("$uid");
if($_SESSION['uid']==$uid)
{
?>
<p>
<form>
<input type="hidden" name="delete_this" id="delete_this<?php echo $i; ?>" value="<?php echo $id; ?>">
<?php print "<b>$fname $lname ($year)</b> $detail"; ?> - <input type="image" src="images/delete.jpg" value="Submit" align="absmiddle" onclick="ajaxFunction<?php echo $i; ?>();" />
</form>
</p>
<?php
}
else
{
print "
<p><b>$fname $lname ($year)</b> $detail</p>
";
}
$i++;
}
?>

 

I am going to hazard a guess that it is to do with this bit of code

 

function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

 

but I have no idea why.

 

Don't know if this is too much or not enough info. Please advise.

 

Anyone? (Thanks for getting this far)

 

regards

simon

Link to comment
Share on other sites

I don't think xmlhttp gets passed to handleServerResponse() automatically.

 

Try having:

function handleServerResponse(xmlhttp) {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

Note that it takes an argument now.

 

And then doing something like this instead:

xmlhttp.onreadystatechange  = function() { 
   handleServerResponse(xmlhttp);
}

Link to comment
Share on other sites

I don't think xmlhttp gets passed to handleServerResponse() automatically.

 

He's declared the "xmlhttp" variable globally, and the fact that the alert is only called once the readyState property == 4, means that can't be the problem. Obviously the problem is the HTTP status is not 200 / OK, possibily 404 / not found? Have you tried running the script without the AJAX layer?

Link to comment
Share on other sites

thanks for looking at this Boom and Adam.

 

Have you tried running the script without the AJAX layer?

 

Ummm, the script actually works with the AJAX layer i.e. the row is actually deleted from the database and the updated database is reflected accurately in the refreshed "widget".

 

The only problem is the alert appears. I can always remove the alert from the script - but, that doesn't seem like the best answer.

Link to comment
Share on other sites

Actually, I need to qualify the statement that the script "works".

 

It does delete the row from the database as it should.

 

However, the url changes on the refresh to something weird like

 

http://localhost/oaa/temp/Tim/pg1.php?delete_this3=48&x=34&y=6

 

when it should just stay the same like this

 

http://localhost/oaa/temp/Tim/pg1.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.