How do I make a chatbox that reads a username from MySQL?
Hey all, so I'm just trying to make a basic chatbox on my site that users will enter in a message and it will display in the box.
To get to the chatbox, users will already be logged into the site. So in the chatbox they wouldnt have to enter开发者_运维百科 in their 'Name'. Just a message.
What would be the best way of doing this?
You wouldn't read the username from the database. If a user is already logged in, then you should have (or should use) a username in $_SESSION["user"]
for example.
Your chatbox could use nothing more than a simple text <input>
that you then receive server-side and store it into the database:
db("INSERT INTO chatbox (user, message, time) VALUES (?,?,?)")
-execute( array($_SESSION["user"], $_POST["input"], time()) );
Which then again is pretty easy to read out and display as a chatbox. (Maybe add a bit of AJAX polling once you have that working.)
精彩评论