Php poll script very useful for question and answer

    Rating: ★★★★☆
    View: 235
    Download: 178
    Từ khóa:

    Tried to minimize index page as much as possible so it will be easy for users to integrate it in their page easily. Database design is also fluid where you can have 2 options as answer or may be 4 or may be 10, whatever you want.

    You just have add all that answers under the same question id. Please note that this script is far different that survey system, survey system includes all the features like textbox, dropdown, textarea and other html controls. php poll script only allows option button, If you want to replace option buttons with checkbox then you have to make few changes to the script.js javascript file.

    I havent implemented that feature i left that for you    I have tried to decorate the element using CSS3 features, I have copied that CSS3 rules from other tutorials as my motto was to design the poll system and not to teach you css3 so i am not going to explain that here.  

     Right now there is no blockage like user can answer only once or so, This is demo version so you can vote as many as times, but if you want to implement that feature you can easily implement it with just one change in select query. Script already stores the user IP address so you dont have to make any other changes to the database. Lets have a look at the code snippets

    HTML Markup

       PHP POll 

    SQL Create:

      `id` int(11) NOT NULL AUTO_INCREMENT,
      `question_id` int(11) NOT NULL,
      `answer` varchar(255) NOT NULL,
      PRIMARY KEY (`id`)

    Some PHP function process in process poll

    function get_questions(){
    	global $conn;	
    	$notIn = "";
    	if(isset($_SESSION['answered']) && count($_SESSION['answered'])){
    		$answered = implode(",",$_SESSION['answered']);
    		$notIn = " where id not in (".$answered.")";
    	//echo "SELECT * FROM `questions` $notIn limit 1";
    	$stmt = $conn->prepare("SELECT * FROM `questions` $notIn limit 1");
    	$question = $stmt->fetch();
    	// add question id to session so we can ignore this question 
    	$_SESSION['answered'][] = $question["id"];
    	$_SESSION['question']   = $question["id"];
    	$stmt = $conn->prepare("SELECT * FROM `answers` where question_id = ?");
    	while($info = $stmt->fetch()){
    		$answers[$info["id"]] = $info["answer"];	
    	echo '{"id":"'.$question["id"].'","question":"'.$question["question"].'","answers":'.json_encode($answers).'}';
    function save_answer($post){
    	global $conn;	
    	$ans  = $post['ans'];
    	$ques = $_SESSION['question'];
    	$stmt = $conn->prepare("insert into poll_answers(question_id,answer_id,user_ip) values(:qid,:aid,:ip)");
    	$success = $stmt->execute(array(
    			":qid" => $ques,
    			":aid" => $ans,
    			":ip" => $_SERVER['REMOTE_ADDR']
    		// fetch and send details back
    		echo get_all_answers($ques);
    	}else echo json_encode(array("success"=>"0","error"=>"Unexpected error occurred"));
    function get_all_answers($qid){
    	global $conn;
    	// get all available answers
    	$ans = answer_options($qid);
    	$stmt = $conn->prepare("SELECT * FROM `poll_answers` where question_id = ?");
    	while($info = $stmt->fetch()){
    		$count[$info['answer_id']] += 1;
    	return json_encode(array("success" => "1","total" => "$answer_count","details" => $count,"opt"=>$ans));
    function answer_options($qid){
    	global $conn;
    	$stmt = $conn->prepare("SELECT * FROM answers where question_id = ?");
    	while($info = $stmt->fetch()){
    		$ans[$info['id']] = $info['answer'];
    	return $ans; 


    loading Đang tải...

    template được ưa chuộng