The best way to change the text on a webpage without refresh

I’m trying to emulate the upvote/downvote system used on the SE sites. Each of my pages have a score which users can upvote or downvote. The arrows are images with onclick links to javascript functions. I need to find a way to dynamically change the score without refreshing the page and then run a script (probably PHP) to …

via PHP Website Development » Search Results » ajax:

The best way to change the text on a webpage without refresh

I’m trying to emulate the upvote/downvote system used on the SE sites. Each of my pages have a score which users can upvote or downvote.
The arrows are images with onclick links to javascript functions. I need to find a way to dynamically change the score without refreshing the page and then run a script (probably PHP) to increment the score in the server’s data files.
Is javascript the best way to do this? I’m not that big of a fan of letting users see the source for my functions.
……………………………………….

This is only possible using Javascript.
Don’t worry about users seeing the source; as long as the server is secure and well-designed, it won’t do any harm.
You should only implement display and validation logic in Javascript; everything must be validated again on the server.
……………………………………….

Welcome to AJAX.
The easiest way to do that is to use jQuery and its $.ajax method.
See http://api.jquery.com/jQuery.ajax/
It’s as simple as
$(‘a.upvote-button’).click(function()
$.ajax(‘/posts/123/upvote’, type: ‘post’);
return false;
});
……………………………………….

Yes, you need javascript (or something considerably less sane) to do this. Namely, you need an AJAX callback.
I’m not that big of a fan of letting users see the source for my functions.
The client-side source of your “function” would be ridiculously simple. It could be as simple as:
$.post(‘/1234567/vote/up’)You may use a direct link to a php script (and form post values) instead, and without a javascript library it would be a few more lines, but you shouldn’t need to expose anything of value in your javascript.
……………………………………….

Use javascript. JQuery is an excellent choice for manipulating on screen content and interacting with a server via asynchronous calls.

For more info: The best way to change the text on a webpage without refresh

PHP Website Development » Search Results » ajax

The best way to change the text on a webpage without refresh

Share this post:

Related Posts

Leave a Comment