Update
Here is the new version of AJAX poll script.

Here you can download the new and updated version of the script. Now tracking all of your web pages automatically. You just have to include it to the desired page.
DEMO
ADMIN
With this package you can:
* Create new polls with unlimited number of fields
* Display multiple polls on a same page
* Vote easily without going out to another page
Installation < 3min - you just have to include 2 lines of code to a desired page.
1. Create table 'polls' in your database
CREATE TABLE IF NOT EXISTS `polls` ( `id` int(4) NOT NULL auto_increment, `title` varchar(50) NOT NULL, `labels` varchar(250) NOT NULL, `votes` varchar(30) NOT NULL, `used_ips` longtext NOT NULL, PRIMARY KEY (`id`) ) ;
2. Set up your server information by editing 'dbinfo.php'
<?php $dbhost = 'localhost'; $dbuser = 'your_database_username'; $dbpass = 'your_database_password'; $dbname = 'your_database_name'; $tableName="polls"; ?>
3. Run 'create_poll.php' in order to create a new poll
Here is how to display the poll:
<?php
$poll_id=1;
include("display_poll.php");
?>
display_poll.php
<?php
include_once("dbinfo.php");
$id=$poll_id;
if (empty($id)) { $id=$_REQUEST['poll_id'];}
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname);
//header('Content-type: text/html; charset=windows-1251');
$sql = "select * from polls where id='$id'"; $query = mysql_query($sql) or die(" Error: ".mysql_error());
$number_rows=mysql_num_rows($query);
$info=mysql_fetch_assoc($query);
$labels = explode("|", $info['labels']); $num_labels=count($labels);
$votes_array = explode("|", $info['votes']);
$num_votes=count($votes_array);
$total=array_sum($votes_array);
$sql = "SELECT * FROM polls WHERE used_ips LIKE '%".$_SERVER['REMOTE_ADDR']."%' AND id='$id' ";
$query = mysql_query($sql) or die(" Error: ".mysql_error());
$voted=mysql_num_rows($query);
$info1=mysql_fetch_assoc($query);
//if not voted
if(!$voted){
?>
<script type="text/javascript">
var http_request = false;
function alertContents(response,ret_el) {
document.getElementById(ret_el).innerHTML = response;
}
function makePOSTRequest(url, parameters, ret_el, callback_function) {
if (typeof callback_function == 'undefined' ) callback_function = alertContents;
var http_request = false;
var activex_ids = ['MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP' ];
if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }
} else if (window.ActiveXObject) { // IE6 and older
for (i = 0; i < activex_ids.length; i++) {
try { http_request = new ActiveXObject(activex_ids[i]); } catch (e) {}
}
}
if (!http_request) { alert('Please update your browser!'); return false; }
document.getElementById(ret_el).innerHTML = "Please wait...";
document.getElementsByTagName("body").item(0).style.cursor="wait";
http_request.onreadystatechange = function() {
if (http_request.readyState !== 4) { return; }
if (http_request.status !== 200) { alert('Please try again later.'); return; }
document.getElementsByTagName("body").item(0).style.cursor="auto";
var response = http_request.responseText;
//setTimeout(function(){ callback_function(response,ret_el)},1000);
callback_function(response,ret_el);
return;
};
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Charset", "windows-1251");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function getVote(int,id)
{
makePOSTRequest('display_poll.php', 'vote=' +int+'&poll_id='+id, 'poll'); }
</script>
<?
if (!isset($_POST['vote'])){
echo "<div id=\"poll\">
<span class=\"open\">".$info['title']."</span>
<form method=\"post\"><fieldset>";
for ($i = 0; $i <= $num_labels-1; $i++) { echo "<input type=\"radio\" name=\"vote\" value=\"$i\" id=\"vote$i\" onclick=\"getVote(this.value,$id)\" /><label for=\"vote$i\">".$labels[$i]."</label><br />";}
echo "</fieldset></form></div>";
}
else {
if (is_numeric($_POST['poll_id']) and is_numeric($_POST['vote']) )
{
$id = $_POST['poll_id'];
$checkIP=unserialize($info1[used_ips]);
if(is_array($checkIP)){array_push($checkIP,$_SERVER['REMOTE_ADDR']);}else{ $checkIP=array($_SERVER['REMOTE_ADDR']);}
$ip=serialize($checkIP);
$vote = $_POST['vote'];
$votes_array[$vote]+=1;
$total=array_sum($votes_array);
$votes = implode("|", $votes_array);
$sql = "UPDATE polls SET votes='$votes', used_ips='$ip' WHERE id='$id'"; $query = mysql_query($sql) or die(" Error: ".mysql_error());
echo "<span class=\"open\">Results:</span><br />Voted:$total<br /><table>";
for ($i = 0; $i <= $num_labels-1; $i++) {
$percent=100*round(@($votes_array[$i]/$total),2);
echo "<tr><td>".$labels[$i]."</td><td align=\"left\"><img style=\"float:left;\" src=\"poll.gif\" width=\"$percent\" height=\"20\"> $percent%</td></tr>";
}
echo "</table>";
}
}
}
else {
echo "<table><tr><td colspan=\"2\"><strong>".$info['title']."</strong></td></tr><tr><td colspan=\"2\">Voted:$total</td></tr> <tr><td colspan=\"2\">Results:</td></tr>";
for ($i = 0; $i <= $num_labels-1; $i++) {
$percent=100*round(@($votes_array[$i]/$total),2);
echo "<tr><td>".$labels[$i]."</td><td align=\"left\"><img style=\"float:left;\" src=\"poll.gif\" width=\"$percent\" height=\"20\"> $percent%</td></tr>";
}
echo "</table>";
}
?>
create_poll.php
<?php
include_once("dbinfo.php");
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname);
if (!empty($_POST["method"])){
$poll_question=$_POST[poll_question];
foreach($_POST as $name => $value) {
if (preg_match("/option/i", $name) and !(empty($value)) ) { $options[].=$value; }
}
if (!empty($options)) {$options=implode("|",$options);} else {echo "Please go back and enter some options for the poll!"; exit;}
$poll_question=mysql_real_escape_string($poll_question);
$options=mysql_real_escape_string($options);
$sql = "INSERT INTO polls (title, labels) VALUES ('$poll_question', '$options')"; $result = mysql_query($sql) or die("err");
if ($result==1) {
echo "Congratulations! Your poll has been created!";
echo "In order to use it just paste the following code to your website:";
$inserted_id=mysql_insert_id();
echo "<span style=\"color:red;\"> <? php \$poll_id=$inserted_id; include(\"display_poll.php\") ?></span>";
}
} else {
?>
<h1>Create new poll</h1>
<form id="myform" name="myform" method="post">
<label for="poll_question">Poll question:</label><input type="text" size="20" id="poll_question" name="poll_question"/> <br /><br />
<ul id="options">
<li><label for="poll_option1">Option1:</label><input type="text" size="20" id="poll_option1" name="poll_option1"/><input type="button" value="Add new option" id="add_option" name="add_option"/></li>
<li><label for="poll_option2">Option2:</label><input type="text" size="20" id="poll_option2" name="poll_option2"/></li>
<li><label for="poll_option3">Option3:</label><input type="text" size="20" id="poll_option3" name="poll_option3"/></li>
<li><label for="poll_option4">Option4:</label><input type="text" size="20" id="poll_option4" name="poll_option4"/></li>
<li><label for="poll_option5">Option5:</label><input type="text" size="20" id="poll_option5" name="poll_option5"/></li>
</ul>
<input type="hidden" name="method" value="post" />
<input type="submit" value="Create!" id="create_poll" name="create_poll"/><br /><br />
</form>
<script type="text/javascript">
function add_new_option()
{
var form = document.getElementById("options");
var new_number = form.childElementCount+1;
var new_id="poll_option"+new_number;
var poll_label = document.createElement("label");
poll_label.htmlFor=new_id;
var p_label_text="Option"+new_number+":";
poll_label.appendChild(document.createTextNode(p_label_text));
var new_option = document.createElement("input");
new_option.type = "text"; new_option.id = new_id; new_option.name = new_id;
var html_li = document.createElement("li");
html_li.appendChild(poll_label);
html_li.appendChild(new_option);
form.appendChild(html_li);
}
if (document.getElementById) { window.onload = myUnobtrusiveBehavior; }
function myUnobtrusiveBehavior() {
document.getElementById("add_option").onclick=function() {add_new_option();}
}
</script>
<?php
}
?>
Note: system works only when JavaScript is enabled.
0 коментара:
Post a Comment