Generally you should do it the following way:
- Attach an event to the button just as @Marc B suggested
- In the function for the event do a
jQuery.ajax
request, sending the users name (type
toPOST
would be a good idea and the users name insidedata
) - On the server create a PHP file (for example
setname.php
) that has the URL you used for the ajax request - In the PHP file take the name from
$_POST['name']
andfile_put_contents
it into any file (let’s saylastbuttonclicker.txt
) - Go back to the JS and make a
window.setInterval(..., 1000);
which runs another JS function - In that function from step 5, do another ajax request (via
jQuery.ajax
and add it viajQuery.fn.text
, notjQuery.fn.html
and don’t usejQuery.fn.load
!!! That way users could write HTML into other users interfaces doing very bad things) to another PHP file (for examplegetname.php
) - In that PHP file just do
<?php echo file_get_contents('lastbuttonclicker.txt'); ?>
But after all be aware, that users can put offensive names there. But that is a topic for itself.
EDIT: You should also ensure, that the content has a suitable length, at the server side (checking inside JavaScript has no impact to bad people writing gigabytes of text to everybodys client and your server).
EDIT 2: <facepalm> Of course you dont need step 7, just load the lastbuttonclicker.txt
itself in step 6 instead of getname.php
solved How would I create a button that links to records on my database? [closed]