Last active
August 29, 2015 13:56
-
-
Save Siot/8831446 to your computer and use it in GitHub Desktop.
Very simple poll with jQuery, PHP and PDO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host="localhost"; //url of database server; | |
$dbname=""; //database name; | |
$user=""; //database username; | |
$pass=""; //database password; | |
$table=""; //database table; | |
//Connection to mysql server; | |
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); | |
if(isset($_REQUEST['answer'])){ | |
# STH means "Statement Handle" | |
$data = array('url' => $_REQUEST['url'], 'answer'=> (int) $_REQUEST['answer']); | |
$STH = $DBH->prepare("INSERT INTO Q_funciona ( url, answer ) values ( :url, :answer)"); | |
$STH->execute($data); | |
} | |
$data = array('url' => $_REQUEST['url']); | |
$STH = $DBH->prepare("SELECT url, answer, COUNT(answer) AS total FROM Q_funciona WHERE url=:url GROUP BY answer"); | |
$STH->execute($data); | |
while($row = $STH->fetch()) { | |
$array[$row['answer']] = $row['total']; | |
} | |
echo json_encode($array); | |
# close the connection | |
$DBH = null; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> | |
<script type="text/javascript" src="survey.js"></script> | |
</head> | |
<body> | |
<div id="survey"> | |
Are you ok? | |
<button value="1">Yes</button> | |
<button value="0">No</button> | |
</div> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//requires jQuery | |
var origin = window.location.protocol + "//" + window.location.host + window.location.pathname; | |
var url = "collector.php"; //url of collector.php; | |
$(document).ready(function() { | |
$("#survey button").on("click", function( event ) { | |
answer = $( this ).val(); | |
xhr_get({url:origin, answer: answer}).done(function(){ | |
$("#survey button").prop("disabled", true) | |
}); | |
}); | |
xhr_get({url:origin}); | |
}); | |
function is_undefined(data){ | |
if(typeof data === "undefined"){ | |
return "0"; | |
}else{ | |
return data; | |
} | |
} | |
function xhr_get(data){ | |
return $.ajax({ | |
type: "POST", | |
dataType: "json", | |
url: url, | |
data: data | |
}).done(function(resp){ | |
if(resp !== null){ | |
$("#survey button[value=1]").text("Sí ("+is_undefined(resp[1])+")"); | |
$("#survey button[value=0]").text("No ("+is_undefined(resp[0])+")"); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment