10
08
Simple Ajax Polling System With PHP, Mysql, jQuery, Ajax

This is one of the simple and flexible free ajax PHP polling script. Simple polling script made by ajax, jQuery, PHP, Mysql, bootstrap. You can display the polls and result on the same page for every votes you can view the answer on the same page. You can skip by answering the next poll by clicking next question. Simple ajax polling script display the polls and result in the same page instead of loading the whole page. It includes the database mysql. The detail of the polling wil be stored and retrived from the database. Currently no restrictions on the polling. You can vote many time as much you can. N number of person votes n no of polls.

simple ajax polling system

simple ajax polling system

Javascript to get and display next poll

function get_poll(){
	loading();
	$("ul").html("");
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data : "act=getq",
		dataType: "json",
		success: function(response){
			var ans1, ans = "";
			if($(".poll").html().length)
			$(".poll").css("display","none");
			if(response.id){
				$.each(response.answers, function(i,val) {

					ans+='
  • '; }); $(".poll").html(""); $(".question").html(response.question); $("ul").append(ans); }else{ $(".next").remove(); $(".question").html(''); $(".button").remove(); $(".poll").fadeIn("slow").html("OOPS. No more question there! :("); } } }); }

    PHP Function to connect database

    $host   = "localhost";
    $dbname = "poll";
    $dbuser = "root";
    $dbpass = "";
    global $conn;
    
    $conn = new PDO("mysql:host=$host;dbname=$dbname","$dbuser","$dbpass");
    

    Function to get all answer

    function get_all_answers($qid){
    	global $conn;
    	// get all available answers
    	$answer_count = 0;
    	$count = array();
    	$ans = answer_options($qid);
    	$stmt = $conn->prepare("SELECT * FROM `poll_answers` where question_id = ?");
    	$stmt->execute(array($qid));
    	$stmt->setFetchMode(PDO::FETCH_ASSOC);  
    	while($info = $stmt->fetch()){
    		$answer_count++;
    		$count[$info['answer_id']] += 1; 
    	}
    	return json_encode(array("success" => "1","total" => "$answer_count","details" => $count,"opt"=>$ans));
    }
    

    By posted on - 10th Aug 2016