Forum Moderators: open
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]
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
<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?
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.
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);
$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!