Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-24-2007, 11:40 PM   PM User | #1
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
Unhappy Crippling- yes, crippling - AJAX question

This question has just about killed my project. (that's why its crippling ) I've debugged it and tried many different bug fixes and observed it and tried to figure out how the heck to fix it so many times that I think my mind is numb to it.

The sad part is, it's not even an error, just a bug that I can't fix.

Okay, now that I've vented a little, I'll actually tell you my problem. I'm in the process of designing a forum, although I'm stuck on the login/logout/register process. An AJAX script takes the registration info from a form, creates an XML document with all the old user info plus the new user, stores that code as a string in a var which gets passed to PHP, and PHP overwrites the old XML file.

So it goes:
- User clicks the register button
- Javascript constructs the code and stores it as a string in the var
- AJAX passes it to PHP
- PHP rewrites the XML
- The page reloads

Very beta so far, no form validation or anything. The problem: The script randomly does not work AT ALL. No users get added or anything. It just doesn't work. Sometimes the xml file is updated. Sometimes its not.

JavaScript/AJAX:
Code:
/////////////////////// AJAX - the bridge between server and client
var AJAXfailmsg = "Browser Error!\nYour browser is too old and does not support the necessary technologies 

for this forum. Try upgrading your browser to one with AJAX support.\n"; // Message for AJAX failure
var forum=loadXMLDoc('forum.xml'); // XML document variable
var numUsers=parseFloat(forum.getElementsByTagName("users")[0].getAttribute("num")); //parseFloat the 

number of users for use in calculations and loop conditions

function loadXMLDoc(dname) // Load XML doc // Dname is the path to the database
{
var xmlDoc; // initialize a var to be used later
if (window.ActiveXObject) // code for IE
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
else if (document.implementation && document.implementation.createDocument) // code for Mozilla, Firefox, 

Opera, etc.
  {
  xmlDoc=document.implementation.createDocument("","",null);
  }
else // no browser AJAX support
  {
  alert(AJAXfailmsg); //Alert with fail message
  }
xmlDoc.async=false;
xmlDoc.load(dname); // load the file
return(xmlDoc); // returns the XML doc when this function is called
}





function ajaxFunction(messageDat){ // the Ajax Request Function
	var ajaxRequest;  // initialize a var for later
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// No AJAX message:
				alert(AJAXfailmsg);
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){ //If the server is done...
			alert("Success!"); // Alert
		}
	}
	// Send it!
	ajaxRequest.open("GET", "forumWrite.php?newXML="+messageDat+"", true); 
	ajaxRequest.send(null); 
}

///////////////////////////// Javascript newXML construction - create the XML and use AJAX to pass it to PHP
// The following 3 functions return a string of XML

function users(typeCall) //Returns the users
{
var users2 = parseFloat(forum.getElementsByTagName("users")[0].getAttribute("num"))+1;
if (typeCall=="old") // If you want the complete old database of users...
{
var usersStr='<users num="'+forum.getElementsByTagName("users")[0].getAttribute("num")+'">';
for (i=0; i<numUsers; i++) // Write user data for each user
{
usersStr+='<user><username>'+forum.getElementsByTagName("username")[i].childNodes[0].nodeValue+'</user

name>';
usersStr+='<password>'+forum.getElementsByTagName("password")[i].childNodes[0].nodeValue+'</password

>';
usersStr+='</user>';
}
usersStr+='</users>';
}
else if (typeCall=="add") // if you want to write the old database and add a new user...
{
var usersStr='<users num="'+users2+'">';
for (i=0; i<parseFloat(forum.getElementsByTagName("users")[0].getAttribute("num")); i++) // Write user data 

for each user
{
usersStr+='<user><username>'+forum.getElementsByTagName("username")[i].childNodes[0].nodeValue+'</user

name>';
usersStr+='<password>'+forum.getElementsByTagName("password")[i].childNodes[0].nodeValue+'</password

></user>';
}
// no </users> tag because you are adding a user: all <user> tags have to be inside the <users> tag...
}
return usersStr; // return the users xml data
}




function config()//Returns the config settings
{
var configsStr = '<config>empty</config>';
return configsStr // return the config xml data
}





function database()//Returns the actual categories, threads, boards, posts, etc.
{
var databaseStr = '<database>empty</database>';
return databaseStr // return the database XML data
}

function registration()
{
var newPHPXML; // This variable will contain new XML data to be written over the old
newPHPXML = '<?xml version="1.0" encoding="UTF-8"?><forum>'; // Beggining data
newPHPXML+=users("add"); // populate with former users,
newPHPXML+='<user><username>'+document.register.username.value+'</username>'; // add new username
newPHPXML+='<password>'+document.register.password.value+'</password>'; // add new password
newPHPXML+='</user></users>' // a few end tags
newPHPXML+=config(); // configs
newPHPXML+=database(); // and the categories, boards, threads, posts, etc.
newPHPXML+='</forum>' // Ending data
var escapedPHPXML = escape(newPHPXML); // newPHPXML escaped for transfer through the _GET superglobal var.

ajaxFunction(escapedPHPXML); // Use AJAX to pass the escaped newPHPXML
document.location=document.location; // goes to another page/reloads
}
HTML:
Code:
<html>
<head>
<script src="func.js"></script> <!-- the above JS file^^^ -->
</head>
<body>
<form name="register">
Username: <input type="text" value="admin" name="username"><br>
Password: <input type="password" value="admin" name="password"><br>
Password Verification: <input type="password" value="admin" name="passwordVerification"><br>
<input type="button" onClick="registration()" value="Register">
</form>
</body>
</html>
PHP: (there's nothing wrong with it, I'm pretty sure. But for reference purposes forumWrite.php:
PHP Code:
<?php
/* The very small PHP frontend function for exBB.*/

if (get_magic_quotes_gpc()) { // If magic quotes are enabled...
   
$newXML stripslashes($_GET['newXML']); // Strip the slashes and get the var
// Slashes before quotes in the XML file will cause errors
} else { // If there not..
   
$newXML $_GET['newXML']; //Get the variable
}

$myFile "forum.xml"// path to the forum database
$fh fopen($myFile'w') or die("Internal forum error!"); // open the database
// Completely overwrites the old data instead of only appending to the end
$stringData $newXML// the data to be written
fwrite($fh$stringData); // actually write it
fclose($fh); // close the file
?>
I am so desperate on this question. It has brought my forum to a screeching halt. I have figured out this:

- It isn't that the AJAX reads the old XML before it is updated


Anyone please help!
binaryWeapon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:29 PM.


Advertisement
Log in to turn off these ads.