Jump to content
xisto Community
Sign in to follow this  
nol

User Integration With A Shoutbox

Recommended Posts

Here is the tutorial I am using:

http://yensdesign.com/2009/01/create-a-shond-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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

so in the line:

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:

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)));

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

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

$query = sprintf("INSERT INTO shoutbox(user, message) VALUES('$_SESSION['username']', '%s');", mysql_real_escape_string(strip_tags($user)),
line
Edited by truefusion (see edit history)

Share this post


Link to post
Share on other sites

Try this (though i don't have the time to test it out):

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

Share this post


Link to post
Share on other sites

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

/***************************///@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro//@website: http://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;	});});

Edited by nol (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.shoutbox.zip

Share this post


Link to post
Share on other sites

Alright so now all I need to do is just edit what was mentioned previously and see if it works? I'll have to test this one i get home later, but thank you very much for that ahead of time. I haven't even tested the user part, but the loading message keeps showing even though I see the shoutbox posts already?

Edited by nol (see edit history)

Share this post


Link to post
Share on other sites
How to make shout pro 1.5.2 available only for memberUser Integration With A Shoutbox

Hi how can make available my shoutbox only for members,what function shoud I create in function.Php for that?, should I make any changes in confing.Php,thanks

-reply by Mircea

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.