Forum Moderators: open

Message Too Old, No Replies

Problems With Ajax driven Chat

         

WhaCo

4:44 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



Hey everybody, First post :)
So, ive been trying to modify this script: <snipped url>
but theres a problem.. I cant put my own variables in the "post section" of the ajax script, Cant get the data into the DB.

This is the post section:

function sendChatText() {
if(document.getElementById('txt_message').value == '') {
alert("You have not entered a message");
return;
}
if (sendReq.readyState == 4 ¦¦ sendReq.readyState == 0) {
sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.onreadystatechange = handleSendChat;
var param = 'message=' + document.getElementById('txt_message').value;
param += '&name=Ryan Smith';
param += '&chat=1';
sendReq.send(param);
document.getElementById('txt_message').value = '';
}
}
What i need to be done here is that i want to add a 4th parameter, Customer, Using PHP.

like this : param +='&customer=<?php echo $customer;?>';
What the customer parameter dose is just defining an ID, wich is defined on the top of my script like this:

<?php
$customer = $_GET['customer'];
?>

And this is what the getChat.php script looks like:

if(isset($_POST['message']) && $_POST['message'] != '') {
$sql = "INSERT INTO message(chat_id, customer, user_id, user_name, message, post_time) VALUES (" .
db_input($_GET['chat']) . ", ".$_POST['customer']." , 1, '" . db_input($_POST['name']) .
"', '" . db_input($_POST['message']) . "', NOW())";
db_query($sql);
}

After i added the "customer part" to both parts of the scripts, NOTHING is passed through to the database.
Any thoughts?

Cheers

[edited by: whoisgregg at 2:44 am (utc) on Sep. 24, 2009]
[edit reason] Whoops, no URLs please. See TOS [webmasterworld.com] :) [/edit]

Demaestro

4:50 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



param +='&customer=<?php echo $customer;?>-;

Javascript is run on the client side so you can't access PHP variables.

You have to somehow pass in the customer id to JS another way.

Try a hard coded way first.

param +='&customer=23';

If that works then you need to figure out how to access customer_id from JS

WhaCo

4:54 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



yeah, ive tryed to hard code it into the script, that works just fine. ive searched the net for a solution but i cant find one. Anyone know how to solve this problem?

Demaestro

5:08 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does the customer_id live in a cookie?

If so you can use that.

Another option is to write the customer_id to the page at load time and then grab it from where you put it.

It all comes down to when you know the customer_id value and passing it to your JS.

How/where is the customer_id determined/set?

WhaCo

5:13 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



No, customer is a session_id determined by OsCommerce.
Looking like this: b5d5b966ae3024a37ed0c599a505809f
i tryed to define a customer var inside the javascript and then alert it, it works just fine.

<script language="JavaScript" type="text/javascript">
var customer = "<?php echo $customer;?>";
alert(customer);
</script>

If that script works ^, Why wont param +='&customer=<?php echo $customer;?>'; work?

Demaestro

5:23 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Leave that alert in, then load the page in FF

-> Select All
-> Right Click selected text
-> Choose "View Selection Source"

Look at the JS function and see what those lines say.

WhaCo

5:29 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



it looks like this:
<script language="JavaScript" type="text/javascript">
var customer = "b5d5b966ae3024a37ed0c599a505809f";
alert(customer);
</script>

in getChat.php i tryed to add a line with the customer id when the script prints out the chat box, as $cust.
<?
$cust = $_GET['customer'];
$last = (isset($_GET['last']) && $_GET['last'] != '') ? $_GET['last'] : 0;
$sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time" .
" FROM message WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > " . $last;
$message_query = db_query($sql);
//Loop through each message and create an XML message node for each.
while($message_array = db_fetch_array($message_query)) {
$xml .= '<message id="' . $message_array['message_id'] . '">';
$xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>';
$xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>';
$xml .= '<time>' . $message_array['post_time'] . $cust . '</time>';
$xml .= '</message>';
}
}
$xml .= '</root>';
echo $xml;

?>

This is the script that sends the customer id to getChat.php through ajax:

//Gets the current messages from the server
function getChatText() {
if (receiveReq.readyState == 4 ¦¦ receiveReq.readyState == 0) {
receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage +'&customer=<?php echo $customer;?>', true);
receiveReq.onreadystatechange = handleReceiveChat;
receiveReq.send(null);
}
}

And that works as well, it prints out the customer ID inside the chatbox.

WhaCo

6:03 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



Tried to put the customer ID into the name variable, and that works as well. it puts the right info inside the Database.
I think it is a MySQL problem.

Whats wrong with this query?:
$sql = "INSERT INTO message(chat_id, customer, user_id, user_name, message, post_time) VALUES (" . db_input($_GET['chat']) . ", ". db_input($_GET['customer']). ", 1, '" . db_input($cust) .
"', '" . db_input($_POST['message']) . "', NOW())";
db_query($sql);

WhaCo

6:05 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



The query above is the wrong one, this is the right one:

$sql = "INSERT INTO message(chat_id, customer user_id, user_name, message, post_time) VALUES (" .
db_input($_GET['chat']) . ", ". db_input($cust) .", 1, '" . db_input($_POST['name']) .
"', '" . db_input($_POST['message']) . "', NOW())";
db_query($sql);

Edit: Solved,

Missed a couple of ' in the query :)
Thanks for all the help!

Demaestro

8:58 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Wha,

Sorry I left you on your own there for a while. I got called away.

Glad you got it working.

The best thing to do when this sort of thing happens is look at the all variables once rendored. That almost always produces a clue.