Nov 22, 2009
Pages: 1, 2

User Integration With A Shoutbox

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > AJAX

User Integration With A Shoutbox

nol
Here is the tutorial I am using:

http://yensdesign.com/2009/01/create-a-sho...nd-ajax-jquery/

I am just wondering how would I, instead of using a form for the username, where they type in their name could I have it where instead the server would automatically post the username they are logged in with into the database? I'm guessing this would be something like the way you would make a user integrated forum system, but I'm not too familiar with ajax nor can I find too many tutorials on the internet for it, so does anybody think they could tell me how I would go about this?

So basically, instead of the user typing in their username, they just need to type in a message, and their username (from the current session) will be posted.

Comment/Reply (w/o sign-up)

truefusion
QUOTE (nol @ Jul 14 2009, 01:53 PM) *
So basically, instead of the user typing in their username, they just need to type in a message, and their username (from the current session) will be posted.

Just take their cookie information since they would already be logged into your site and modify the PHP shoutbox script to reflect their cookie information. AJAX isn't required for this, and would be better if not used for inserting their user name into the user name field.

Comment/Reply (w/o sign-up)

nol
so in the line:

CODE
function insertMessage($user, $message){
    $query = sprintf("INSERT INTO shoutbox(user, message) VALUES('%s', '%s');", mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message)));


Should I add something like:

$_SESSION['username']

Into where it says values of user (%s)?

so it would look like:

CODE
function insertMessage($user, $message){
    $query = sprintf("INSERT INTO shoutbox(user, message) VALUES('$_SESSION['username']', '%s');", mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message)));

 

 

 


Comment/Reply (w/o sign-up)

truefusion
QUOTE (nol @ Jul 14 2009, 02:39 PM) *
Should I add something like:

$_SESSION['username']

Into where it says values of user (%s)?

so it would look like:

Basically, but this assumes the user is always logged in. Also, make sure you have filtered that session variable and that the session variable gets parsed within the string.

Comment/Reply (w/o sign-up)

nol
QUOTE (truefusion @ Jul 14 2009, 08:17 PM) *
Basically, but this assumes the user is always logged in. Also, make sure you have filtered that session variable and that the session variable gets parsed within the string.

As in the session starts and includes that the user must be logged in? I've got session starting, and I've got it so if the user is not logged in, it will redirect them to the login page, otherwise it shows the shoutbox. I'll try it I haven't added it in just yet though.

So when I try and include my function.php I get

QUOTE
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/openpoli/public_html/shoutbox.php on line 40


which is my

CODE
    $query = sprintf("INSERT INTO shoutbox(user, message) VALUES('$_SESSION['username']', '%s');", mysql_real_escape_string(strip_tags($user)),
line

Comment/Reply (w/o sign-up)

truefusion
Try this (though i don't have the time to test it out):
CODE
$query = sprintf("INSERT INTO shoutbox(user, message) VALUES('".$_SESSION['username']."', '%s');", mysql_real_escape_string(strip_tags($message)));

Comment/Reply (w/o sign-up)

nol
So, I added that, and then it wouldn't let me try to test it because it would say that I needed to fill out all the fields, so on shoutbox.js I took out

CODE
    var inputUser = $("#nick");


and now it seems like it won't even add any more shouts? I press enter, it shows the loading image, but it doesn't show the new shout?

Also here is the shoutbox.js script before I did anything to it

CODE
/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

$(document).ready(function(){
//global vars
var inputUser = $("#nick");
var inputMessage = $("#message");
var loading = $("#loading");
var messageList = $(".content > ul");

//functions
function updateShoutbox(){
//just for the fade effect
messageList.hide();
loading.fadeIn();
//send the post to shoutbox.php
$.ajax({
type: "POST", url: "shoutbox.php", data: "action=update",
complete: function(data){
loading.fadeOut();
messageList.html(data.responseText);
messageList.fadeIn(2000);
}
});
}
//check if all fields are filled
function checkForm(){
if(inputUser.attr("value") && inputMessage.attr("value"))
return true;
else
return false;
}

//Load for the first time the shoutbox data
updateShoutbox();

//on submit event
$("#form").submit(function(){
if(checkForm()){

var nick = inputUser.attr("value");
var message = inputMessage.attr("value");
//we deactivate submit button while sending
$("#send").attr({ disabled:true, value:"Sending..." });
$("#send").blur();
//send the post to shoutbox.php
$.ajax({
type: "POST", url: "shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message,
complete: function(data){
messageList.html(data.responseText);
updateShoutbox();
//reactivate the send button
$("#send").attr({ disabled:false, value:"Shout it!" });
}
});
}
else alert("Please fill all fields!");
//we prevent the refresh of the page after submitting the form
return false;
});
});

Comment/Reply (w/o sign-up)

truefusion
If you're using JavaScript to verify if the fields were filled, you should avoid doing so, as it's not the right way to go about it. If you have the PHP script verify if the fields are field, then that is the right way to go about it, but that would mean the problem was in the PHP script. If you followed the tutorial to the dot, then perhaps the tutorial was too simple for what you wanted to accomplish. I haven't analyzed the tutorial to see how complex it is, and also to see what methods it uses to verify user input, but the information you provided on your current problem isn't enough to help you solve the problem. If you have more than basic knowledge in PHP or JavaScript, then you should be able to solve this problem, but if you're looking for us to solve the problem for you, you'll have to provide way more information.

Edit: posted a bit late. I'll analyze it in a bit.

Comment/Reply (w/o sign-up)

nol
Should I post the whole php page's script? And should I take out the part on the javascript page and make it check on the php page not javascript? Also the whole script source is hosted on that website too, it isn't just the tutorial.

Comment/Reply (w/o sign-up)

truefusion
Here's a modified version of the script. Since i am uncertain how you keep track of a user's log-in information, the only lines you would have to modify to consider logged-in users are lines 60 and 61, and you'll also have to re-insert the database information in lines 12 through 15, of shoutbox.php. The differences between this code and the one provided on the website you referenced include: it now works without JavaScript enabled, it considers whether or not magic quotes are on, i renamed index.html to index.php, and i removed a few unnecessary things.

[attachment=1703:shoutbox.zip]

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2
Similar Topics

Keywords : User Integration Shoutbox


    Looking for User, Integration, With, A, Shoutbox

Searching Video's for User, Integration, With, A, Shoutbox
See Also,
advertisement


User Integration With A Shoutbox

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com