PDA

View Full Version : [OFFICAL] Web Development and Coding Support!



Hunter
April 11th, 2010, 05:00 PM
Web Development and Coding Support Thread
Post problems surrounding coding and design here. PHP, JS, AJAX, HTML, CSS, XML ect...

Thread would make a nice sticky in my opinion. Then people like me won't
need to make a load of threads for every problem that they have, but hey, that is my opinion and is usually wrong or ignore lmao. :haw:

--------------------------------------------------------------------

Problem/Error: split() function adding commer "," on the end of result

Fix highlighted in blue.

JavaScript:


function CSSchange(elm, class, apend, to) {
//If set to false the class variable will be taken from the final name
if (to == true) {
//If set to true then the class varialbe will be ADDED
if (apend == true) {
elm.className = elm.className+class;
} else {
//Selected element will have its class CHANGED
elm.className = class;
}
} else {
//Selected element will have the class variable deleted from final class name
elm.className = elm.className.split(class,1);
DEL>alert(elm.className.split(class));
}
}
HTML:


<div class="chat1" onmouseover="CSSchange(this, '_hover', true, true)" onmouseout="CSSchange(this, '_hover', true, false)">
Martyn Ball
</div>


Problem:
The alert displays "chat1,". The commer should not be there and I can't see where it is getting it from :maddowns:

Edit: Just tryed this: elm.className = elm.className.split(class,1); and it worked again :/ Weird. Keeping fix here anyway for future problems people may have.

What am I trying to do?
Well, basically make a rollover button, which will eventually do more when it is clicked, details are not needed.

This function could be used to make other rollover buttons, CSS has an attribute which can be added at the end of a style such as: button:hover / button:active. However because Internet Explorer is complete shit, this attribute usually ONLY works on links (<a></a>), although FireFox, Chrome and other browsers have no problem with this being used on other elements.

So this function could be used on elements such as dividers to swap their class so they will look different.

Hunter
April 12th, 2010, 08:34 AM
Problem/Error: JavaScript/AJAX not returning values correctly...

The JavaScript/AJAX is printing "null" into the chat_div where the tag name is "user". But in those tags there is actually data, this data is a link. I am thinking that it may be returning the first childNode which would be the <a>? I don't fully understand nodes, but I have had a quick read about them.

Fixed:

Fixed it, the main reason it didnt work was because I was using an illegal character which XML does not allow, which is "<". These kind of characters should be replaced with: $lt;

Extract from W3Schools:


&lt; < less than
&gt; > greater than
&amp; & ampersand
&apos; ' apostrophe
&quot; " quotation mark
------------------------------

Fix:
Highlighted in blue in the JS.

JavaScript/AJAX:


function manageMessage() {
if( receiveReq.readyState == 4 ) { // request complete
var chat_div = document.getElementById( 'chat' );
var xmldoc = receiveReq.responseXML;
var msg_nodes = xmldoc.getElementsByTagName( 'message' );
var n_msg = msg_nodes.length;
for( i = 0; i < n_msg; i++ ) {
var user_node = msg_nodes[ i ].getElementsByTagName( 'user' );
var time_node = msg_nodes[ i ].getElementsByTagName( 'time' );
var text_node = msg_nodes[ i ].getElementsByTagName( 'text' );

//Get name and ID
var name_id = user_node[0].firstChild.nodeValue.split(".", 2);

//Display message
chat_div.innerHTML += "<a href=\"profile.php?id="+ name_id[0] +"\" target=\"_blank\">"+ name_id[1] + "</a><br />";
chat_div.innerHTML += time_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += text_node[ 0 ].firstChild.nodeValue + "<br /><br />";
}
}
}


PHP:


//While result is being got get first name and upper case first letter.
while ($row2=mysql_fetch_array($get_name)) {
$name = explode(".",$row2['full_name']); $first_name = $name[0];
$userid = $row2['uid'];
$sent_by = $userid.".".ucfirst($first_name);
}

//Write message contents
........


PHP:


<?php
//Headers
header("Expires: Mon, 01 Dec 1990 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-Type: text/xml; charset=utf-8");

require("connect.php");
mysql_select_db($dbname, $con);

//Variables
$ses = explode(".", $_SESSION['user']);
//User ID of user currently logged in
$sid = $ses[2];

//Number of last message which was displayed, if does not exist or has no value then
//last message will be 0.
$last_mess = (isset($_GET['m']) && $_GET['m'] != "") ? $_GET['m'] : 0;
$last_mess = mysql_real_escape_string($last_mess);

$chat = mysql_real_escape_string($_GET['chat']);
//Counter for counter number of errors
$i = 1;

//Send message
if (isset($_POST['message']) && $_POST['message'] != "") {
//Variables
$chat = mysql_real_escape_string($_GET['chat']);
$message = mysql_real_escape_string($_GET['message']);

$query = "INSERT INTO message (chat_id, user_uid, recipient_uid, message, post_time) VALUES (".
$chat.", ".$sid.", ".$rid.", ".$message.", NOW())";
mysql_query($query);
}

//Get current chat
if (isset($_GET['chat'])) {
$xml = '<?xml version="1.0" ?><root>';
$query = "SELECT * FROM message WHERE chat_id = ".$chat." AND msg_id > ".$last_mess;
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)) {

//Get name of sender
$get_name_query = "SELECT * FROM users WHERE uid = '".$row['user_uid']."'
OR uid = '".$row['recipient_uid']."'";
$get_name = mysql_query($get_name_query);
if (!$get_name) { //If there is no result display error
echo "Error $i: Error getting usernames! <br /> Query: $get_name_query <br />MySQL Error: ".mysql_error().
"<br /><br />";
$i++;
}
//While result is being got get first name and upper case first letter.
while ($row2=mysql_fetch_array($get_name)) {
$name = explode(".",$row2['full_name']); $first_name = $name[0];
$userid = $row2['uid'];
$sent_by = "<a href=\"../profile.php?user=".$userid."\">".ucfirst($first_name)."</a>";
}

//Write message contents
$xml .= '<message id="' . $row['msg_id'] . '">';
$xml .= '<user>' . $sent_by . '</user>';
$xml .= '<text>' . $row['message'] . '</text>';
$xml .= '<time>' . $row['post_time'] . '</time>';
$xml .= '</message>';
}
$xml .= '</root>';
echo $xml;
}
?>
JavaScript/AJAX:


function getMessage() {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
var url = "scripts/get_data.php?uid=<?=$uid?>&m=" + lastMess + "&chat=1";
receiveReq.open("GET", url, true);
receiveReq.onreadystatechange = manageMessage;
receiveReq.send( null );
lastMess++;
}
}

function manageMessage() {
if( receiveReq.readyState == 4 ) { // request complete
var chat_div = document.getElementById( 'chat' );
var xmldoc = receiveReq.responseXML;
var msg_nodes = xmldoc.getElementsByTagName( 'message' );
var n_msg = msg_nodes.length;
for( i = 0; i < n_msg; i++ ) {
var user_node = msg_nodes[ i ].getElementsByTagName( 'user' );
var time_node = msg_nodes[ i ].getElementsByTagName( 'time' );
var text_node = msg_nodes[ i ].getElementsByTagName( 'text' );
//Display message
chat_div.innerHTML += user_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += time_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += text_node[ 0 ].firstChild.nodeValue + "<br /><br />";
}
}
}


More info:

The PHP is displaying the data properly, like so:

XML:


<root>
<message id="1">
<user>
<a href="../profile.php?user=4">Nicholas</a>
</user>
<text>testing stupid chat thing</text>
<time>0000-00-00 00:00:00</time>
</message>
<message id="2">
<user>
<a href="../profile.php?user=4">Nicholas</a>
</user>
<text>dsadasfsdfsggsdgrgsdgfd</text>
<time>2010-04-22 19:17:16</time>
</message>
<message id="3">
<user>
<a href="../profile.php?user=4">Nicholas</a>
</user>
<text>gfgdgsegegsgfawdfwdfasfa</text>
<time>2010-04-14 17:28:07</time>
</message>
</root>
What is being displayed on the page:


null
0000-00-00 00:00:00
testing stupid chat thing

null
2010-04-22 19:17:16
dsadasfsdfsggsdgrgsdgfd

null
2010-04-14 17:28:07
gfgdgsegegsgfawdfwdfasfa