Feature Suggest App w/ PHP, MySQL & jQuery

Feature Suggest App w/ PHP, MySQL & jQuery

Listening to what your visitors have to say, is always beneficial when planning new features or changes in your website. For a long time we’ve been limited to just setting up a contact form and hoping that quality feedback will follow, which unfortunately is not always the case.

Today we are taking things up a notch – we are applying the same social principles that have brought success to sharing sites such as Digg and delicious, and encourage visitors to suggest and vote on features that they want implemented on your website.


Starting with the new HTML5 doctype, we define the opening and closing head and title tags, and include the main stylesheet of the app – styles.css, in the document.


<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Feature Suggest w/ PHP, jQuery & MySQL | Tutorialzine Demo</title>

<link rel="stylesheet" type="text/css" href="styles.css" />



<div id="page">

    <div id="heading" class="rounded">
    	<h1>Feature Suggest<i>for Tutorialzine.com</i></h1>

    <!-- The generated suggestion list comes here -->

    <form id="suggest" action="" method="post">
            <input type="text" id="suggestionText" class="rounded" />
            <input type="submit" value="Submit" id="submitSuggestion" />


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="script.js"></script>

After this comes the body tag and the #page div, which is the main container element. It holds the heading, the unordered list with all the suggestions (which is generated by PHP, as you will see in a moment), and the submit form.

Lastly we include the jQuery library from Google’s AJAX Library CDN, and our own script.js file, which is discussed in detail in the last section of this tutorial.

Feature Suggest App w/ jQuery, PHP, MySQL

Feature Suggest App w/ jQuery, PHP, MySQL

The Table Schema

The app uses two MySQL tables to store data. Suggestions and Suggestions_votes. The first table contains the text of the suggestion and data such as rating and the number of votes the item has received. The second table keeps record of the IPs of the voters and prevents more than one vote to be cast in a single day per IP.

Suggestion Table Schema

Suggestion Table Schema

To speed up the selection queries, an index is defined on the rating field. This helps when showing the suggestions ordered by popularity.

The suggestion votes table has a primary key consisting of three fields – the suggestion_id, the IP of the voter, and the date of the vote. And because primary keys do not allow for duplicate rows, we can be sure that users can vote only once per day by just checking the value of the affected_rows variable after the insert.

Suggestions_Votes Schema

Suggestions_Votes Schema


Before delving into the generation of the suggestion items and the AJAX interactions, first we have to take a look at the suggestion PHP class. It uses two PHP magic methods (apart from the constructor) to provide rich functionality to our code. When generating the front page, PHP runs a MySQL select query against the database, and creates an object of this class for every table row. The columns of the row are added as properties to the object.


class Suggestion
	private $data = array();

	public function __construct($arr = array())

			// The $arr array is passed only when we manually
			// create an object of this class in ajax.php

			$this->data = $arr;

	public function __get($property){

		// This is a magic method that is called if we
		// access a property that does not exist.

			return $this->data[$property];

		return NULL;

	public function __toString()
		// This is a magic method which is called when
		// converting the object to string:

		return '
		<li id="s'.$this->id.'">
			<div class="vote '.($this->have_voted ? 'inactive' : 'active').'">
				<span class="up"></span>
				<span class="down"></span>

			<div class="text">'.$this->suggestion.'</div>
			<div class="rating">'.(int)$this->rating.'</div>

The __toString() method is used to create a string representation of the object. With its help we can build the HTML markup, complete with the suggestion title and number of votes.

The __get() method is used to route the access to undefined properties of the class to the $data array. This means that if we access $obj->suggestion, and this property is undefined, it is going to be fetched from the $data array, and returned to us as if it existed. This way we can just pass an array to the constructor, instead of setting up all the properties. We are using this when creating an object in ajax.php.

Now lets proceed with the generation of the unordered list on the front page.


require "connect.php";
require "suggestion.class.php";

// Converting the IP to a number. This is a more effective way
// to store it in the database:

$ip	= sprintf('%u',ip2long($_SERVER['REMOTE_ADDR']));

// The following query uses a left join to select
// all the suggestions and in the same time determine
// whether the user has voted on them.

$result = $mysqli->query("
	SELECT s.*, if (v.ip IS NULL,0,1) AS have_voted
	FROM suggestions AS s
	LEFT JOIN suggestions_votes AS v
		s.id = v.suggestion_id
		AND v.ip = $ip
	ORDER BY s.rating DESC, s.id DESC

$str = '';

	// Generating the UL

	$str = '<ul class="suggestions">';

	// Using MySQLi's fetch_object method to create a new
	// object and populate it with the columns of the result query:

	while($suggestion = $result->fetch_object('Suggestion')){

		$str.= $suggestion;	// Uses the __toString() magic method.


	$str .='</ul>';

After running the query, we use the fetch_object() method of the $result object. This method creates an object of the given class for every row in the result, and assigns the columns of that row to the object as public properties.

PHP also manages the AJAX requests sent by jQuery. This is done in ajax.php. To distinguish one AJAX action from another, the script takes a $_GET[‘action’] parameter, which can have one of two values – ‘vote‘ or ‘submit‘.


require "connect.php";
require "suggestion.class.php";

// If the request did not come from AJAX, exit:

// Converting the IP to a number. This is a more effective way
// to store it in the database:

$ip	= sprintf('%u',ip2long($_SERVER['REMOTE_ADDR']));

if($_GET['action'] == 'vote'){

	$v = (int)$_GET['vote'];
	$id = (int)$_GET['id'];

	if($v != -1 && $v != 1){

	// Checking to see whether such a suggest item id exists:
	if(!$mysqli->query("SELECT 1 FROM suggestions WHERE id = $id")->num_rows){

	// The id, ip and day fields are set as a primary key.
	// The query will fail if we try to insert a duplicate key,
	// which means that a visitor can vote only once per day.

		INSERT INTO suggestions_votes (suggestion_id,ip,day,vote)

	if($mysqli->affected_rows == 1)
			UPDATE suggestions SET
				".($v == 1 ? 'votes_up = votes_up + 1' : 'votes_down = votes_down + 1').",
				rating = rating + $v
			WHERE id = $id

else if($_GET['action'] == 'submit'){

	// Stripping the content
	$_GET['content'] = htmlspecialchars(strip_tags($_GET['content']));


	$mysqli->query("INSERT INTO suggestions SET suggestion = '".$mysqli->real_escape_string($_GET['content'])."'");

	// Outputting the HTML of the newly created suggestion in a JSON format.
	// We are using (string) to trigger the magic __toString() method.

	echo json_encode(array(
		'html'	=> (string)(new Suggestion(array(
			'id'			=> $mysqli->insert_id,
			'suggestion'	=> $_GET['content']

When jQuery fires the ‘vote‘ request, it does not expect any return values, so the script does not output any. In the ‘submit‘ action, however, jQuery expects a JSON object to be returned, containing the HTML markup of the suggestion that was just inserted. This is where we create a new Suggestion object for the sole purpose of using its __toString() magic method and converting it with the inbuilt json_encode() function.

Suggest & Vote on Features

Suggest & Vote on Features

The jQuery

All of the jQuery code resides in script.js. It listens for click events on the green and red arrows. But as suggestions can be inserted at any point, we are using the live() jQuery method, so we can listen for the event even on elements that are not yet created.



	var ul = $('ul.suggestions');

	// Listening of a click on a UP or DOWN arrow:

	$('div.vote span').live('click',function(){

		var elem		= $(this),
			parent		= elem.parent(),
			li			= elem.closest('li'),
			ratingDiv	= li.find('.rating'),
			id			= li.attr('id').replace('s',''),
			v			= 1;

		// If the user's already voted:

			return false;


			v = -1;

		// Incrementing the counter on the right:
		ratingDiv.text(v + +ratingDiv.text());

		// Turning all the LI elements into an array
		// and sorting it on the number of votes:

		var arr = $.makeArray(ul.find('li')).sort(function(l,r){
			return +$('.rating',r).text() - +$('.rating',l).text();

		// Adding the sorted LIs to the UL

		// Sending an AJAX request


		var form		= $(this),
			textField	= $('#suggestionText');

		// Preventing double submits:
		if(form.hasClass('working') || textField.val().length<3){
			return false;



				// Appending the markup of the newly created LI to the page:

		return false;

When a click on one of those arrows occurs, jQuery determines whether the ‘inactive’ class is present on the LI element. This class is only assigned to the suggestion, if the user has voted during the last day, and, if present, the script will ignore any click events.

Notice how $.makeArray is used to turn the jQuery objects, containing the LI elements, into a true array. This is done, so we can use the array.sort() method and pass it a custom sort function, which takes two LIs at the same time and outputs a negative integer, zero or a positive integer depending on which of the two elements has a grater rating. This array is later inserted into the unordered list.


Now that we have all the markup generated, we can move on with the styling. As the styling is pretty much trivial, I only want to show you the class that rounds the top-left and bottom-right corners of the elements that it is applied to. You can see the rest of the CSS rules in styles.css.


.suggestions li{



Notice that the Mozilla syntax differs from the standard in the way it targets the different corners of the element. Keeping that in mind, we can apply this class to pretty much every element, as you can see from the demonstration.

With this our Feature Suggest App is complete!


If you plan to set up this script on your own server, you would need to create the two suggestion tables by running the code found in tables.sql in the SQL tab of phpMyAdmin. Also remember to fill in your database connection details in connect.php.

You can use this script to gather precious feedback from your visitors. You can also disable the option for users to add new suggestions, and use it as a kind of an advanced poll system.

Be sure to share your thoughts in your comment section below.

Presenting Bootstrap Studio

a revolutionary tool that developers and designers use to create
beautiful interfaces using the Bootstrap Framework.

Learn more
Web Browser Frame DevKit Box Mouse Cursor
by Martin Angelov

Martin is a web developer with an eye for design from Bulgaria. He founded Tutorialzine in 2009 and it still is his favorite side project.

1Share this post
2Read one more article
3Get your free book
Book Cover
jQuery Trickshots

Tutorialzine's advanced jQuery techniques book.



  1. MFlorin says:

    Awesome!! As Always!! Best tutorials from the net!!

    Good Job!

  2. Shay says:

    Nice :) thats what i actually wanted (y)

  3. Gurrewe says:

    Totally awesome!

  4. edurup says:

    very very nice work thanks a lot

  5. Brad Vincent says:

    I love it!
    I would really love a way to moderate the new feature suggestions as I think people are going to add alot of spam.

    Make it visible to the guy who adds it, but its not visible to others until it has been moderated.

  6. schiwe says:

    Quality tutorials, as always.

  7. Q_the_novice says:

    @schiwe: i agree 100%

  8. Martin Angelov says:

    @ Brad

    Yes, spam could become a big problem. One way you could deal with it, is holding all the suggestions for moderation (but there is a big chance that you will get tired of having to moderate every single suggestion).

    I think a more elegant solution would be to implement Akismet (the same anti-spam that guards WordPress blogs). It is free, and if you own a blog, you probably already have an API key.

    It would be a great material for a write up (or a microtut), but I will come round to doing it after I release Tzine's redesign next week (yep, that suggestion is actually real and I am happy that it held up in the Top 5).

  9. Taco says:

    Works great and thanks for sharing the tutorial. But is a app like GetSatisfaction.com not more useful? There is no real interaction with your users this way.

  10. Sanid says:

    Is it possible to make a To Do-App out of this? would be perfect.

  11. Rares says:

    Very good material!

    However, when trying this on my server, the returned suggestion title was displayed with all the single quotes escaped.

    To fix this I added:

    msg.html = msg.html.replace(/\\\'/g,'\''); 

    just before the line with the appendTo(...) call.

    Thank you!

  12. Pretty cool idea and very well implemented..

  13. veeru says:

    always delivers top class tutorials.Thank you..

  14. Jerry Jones says:

    You know, I was wondered, how you guys distributed all these quality stuff for FREE while others are charging for this!
    What a wonder usages of jQuery! Am just loving it.

    BTW, is there any way to control the row being displayed. Like, if we don't have much space, then we would love to display only latest 5-10 rows. And also there must be a scrolling function added to this so that we can scroll down and see the old posts.

    Any help?

  15. awesome plugin. I have seen same type of system in Google support forum.

  16. supachet says:

    nice Tutorials Thank

  17. orhangut says:

    Awesome!... :)
    thank you

  18. Santosh says:

    Awesome stuff guys. Being a web developer, I follow this blog regularly. Starting from fancy 404 page to latest python tutorials , everything here is well documented and very well explained.

    Hats off to the tutorialzine team !

    Thanks for sharing such a nice stuff. :)

    Please keep on sharing.

  19. Tim says:

    How would I go about adding just one thing that might say, "Do you like this website design?" with the thumbs up/thumbs down but no suggestions. Just the one I put in.
    Thanks for any help.
    Great tutorials.

  20. Ken says:

    Can you (or anyone) explain this section of suggestions.php:

    $result = $mysqli->query("
    	SELECT s.*, if (v.ip IS NULL,0,1) AS have_voted
    	FROM suggestions AS s
    	LEFT JOIN suggestions_votes AS v
    		s.id = v.suggestion_id
    		AND v.day = CURRENT_DATE
    		AND v.ip = $ip
    	ORDER BY s.rating DESC, s.id DESC

    I understand the AS statements for s and v, but I'm not following what "(v.ip IS NULL,0,1) AS have_voted" is doing. Perhaps if someone could redescribe this code without the the AS statements I would find it clearer. Thank you for this wonderful tutorial.

    1. Martin Angelov says:

      The code you pointed is actually an if statement:

      if (v.ip IS NULL,0,1) AS have_voted

      When v.ip IS NULL evaluates to true (no ip address found, the user hasn't voted), the if statement returns 0 (the second argument), otherwise the return value is 1 (the third argument). The AS statement, or alias, turns this value into a have_voted field, which will be available to our PHP code.

  21. This is a great tutorial. I really like how mine turned out. It works great!

  22. Freebie#1 says:

    Hi Martin , first of all , great tutorial . Im gonna use it in my website, but, i want to modify one option, i want, the user vote one option, like a poll, i hope you can tell me how i can modify it, thanks.

  23. Pimp Yannick says:

    What should I add if I want to include the name?

  24. vladimire says:

    It won't work after I upgrade to latest Firefox version, I get a security warning whenever someone tries to add a suggestion:

    uncaught exception: [Exception... "Security error" code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)" location: "http://maroc.voxpopolo.com/js/scripts.js Line: 717"]
    Line 138

    It works fine on IE though.. --"

  25. Frank says:

    Hi, great tutorial, i was just wondering, which parts of the code would i have to edit to make this work with older mysql. I know mysqli is better, but im using a user login system currently that doesnt use mysql, and they dont seem to work well together


  26. nit says:

    Hi Martin, I've been going through your tutorials for a while now.. definitely learning a lot, thanks for having this site. :)

    I'm having some issues with this one though, not really sure what the problem is but I can't get it to run. I get a blank page when I open suggestions.php on my localhost.

    I've been googling around for solutions for the past few days, no luck. I tried enabling PHP error tracking, went though all the files checking for missing semicolons, braces etc. still no luck.

    What am I missing? Any help would be greatly appreciated.

    1. Martin Angelov says:

      A blank screen usually happens if there is an error present but errors are suppressed. Check your error log files, you should find an error description there.

  27. nit says:

    Thanks Martin. Had to enable the 'mysqli' extension in the php.ini file. All good now. :)

  28. technotim says:

    Hi. It loads on my site and I can make suggestions but as soon as i refresh the page they are gone. Any ideas?

  29. steve says:

    Great script! Thank you for sharing. Can this be modified so that only one suggestion can be voted for, then all the other voting buttons become inactive? I've tried changing the javascript in the event listener when a vote is cast, but I don't know how to loop through all the LI elements to change the class from active to inactive.

  30. johanso says:

    very nice work thanks, Awesome!!

Add Comment

Add a Reply

HTML is escaped automatically. Surround code blocks with <pre></pre> for readability.
Perks:   **bold**   __italics__   [some text](http://example.com) for links