Hi,

I was looking for a poll script (php/ajax) that does not reload when adding a vote.

I've tried to search around for it but haven't found one that suits my needs. Anyone that knows where I can find such a script?

Thanks,

Robin

Recommended Answers

All 9 Replies

are u trying to store the results aka each vote in a mysql database or a text file?

thanks for your reply..

it doesnt really matter..

im sorry i dont understand it at all but i asked my brother and he has some sucessful ajax thing on his site that does something with the database then he directed me to the tizag website cause i was interested in a poll like think ajax style myself. but well i JUST CANT COMPREHEND IT but heres the link that has to do with updating the mysql database. it probably contains the ajax parts but its not a poll tutorial just ajax and database which may be useful:
http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php
i reread it and the actual aajax part is on the other page. im kind of understanding it but i still dont know how to rework it into a poll exactly. i guess the variables are posted to the php page the ajax runs (in the background or something) which queries the database which in a poll case would insert the vote into the like table thing and log the ip or something so the same ip cant vote over and over again?

Thanks, I will take a look at it..

I searched around for it on Google and actually managed to find a fully programmed script but unfortunately I closed down my web browser without boookmarking the page and when I now try to search again for it I can't find anything.. :S

Thanks, I will take a look at it..

I searched around for it on Google and actually managed to find a fully programmed script but unfortunately I closed down my web browser without boookmarking the page and when I now try to search again for it can't find anything.. :S

argh! i hate that! LOL

Heh.. yeah, it's happend to be before as well... almost embarrassing that I never seem to learn.. :S

ok this is what ive got to start:
pollscript.php:

<?php
require($_SERVER["DOCUMENT_ROOT"] . "/my connect to database file.php");
	// Retrieve data from Query String
$pollid = $_GET['pollid'];
$vote = $_GET['vote'];
	// Escape User Input to help prevent SQL Injection
$pollid = mysql_real_escape_string($pollid);
$vote = mysql_real_escape_string($vote);
	//build query
if(is_numeric($pollid) && is_numeric($vote)){$query = "UPDATE polls SET vote = `vote`+'$vote' WHERE pollid = '$pollid'";
$qry_result = mysql_query($query) or die(mysql_error());

	//Build Result String
$display_string = "thanks for letting everybody know hon.";
	
}
echo "Query: " . $query . "<br />";
echo $display_string;
?>

order.html:

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var pollid = document.getElementById('pollid').value;
	var vote = document.getElementById('vote').value;
	var queryString = "?pollid=" + pollid + "&vote=" + vote;
	ajaxRequest.open("GET", "pollscript.php" + queryString, true);
	ajaxRequest.send(null); 
}

//-->
</script>




<div id='ajaxDiv'>
<form name='myForm'>
<input type='hidden' id='pollid' value='55'/> 
<select id='vote'>
<option value='1'>1 aka yes u like</option>
<option value='-1'>-1 aka no u hate it</option>
</select>
<input type='button' onclick='ajaxFunction()' value='add or minus one on the database for this poll id' />
</form>
</div>
</body>
</html>

ok this is better than what i had before this:

[IMG]http://i46.tinypic.com/29kuss7.png[/IMG]
makepoll.php:

<?php 
require($_SERVER["DOCUMENT_ROOT"] . "/file to conect to mysql database.php"); if(isset($_POST[submitpoll])){echo "submitted";
$category = mysql_real_escape_string($_POST[category]);
$poll = mysql_real_escape_string($_POST[poll]);
$ontime = time();
$insertpoll = mysql_query("INSERT INTO polls (`bywhom`, `ontime`, `category`, `vote`, `poll`) VALUES ('$_SESSON[id]', '$ontime', '$category', '0', '$poll')");
if($insertpoll){echo "successfully created poll";}
} ?>

<form action="makepoll.php" method="post">
category:<select name="category">
<option value="1">category 1</option>
<option value="2">category 2</option>
</select>
<br>
poll question:
<textarea name="poll"></textarea>
<input type="submit" name="submitpoll" value="make poll">
</form>

vote.php:

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var pollid = document.getElementById('pollid').value;
	var vote = document.getElementById('vote').value;
	var queryString = "?pollid=" + pollid + "&vote=" + vote;
	ajaxRequest.open("GET", "pollscript.php" + queryString, true);
	ajaxRequest.send(null); 
}

//-->
</script>




<div id='ajaxDiv'>
<form name='myForm'>
<input type='hidden' id='pollid' value='4'/> 
<select id='vote'>
<option value='1'>1 aka yes u like</option>
<option value='-1'>-1 aka no u hate it</option>
</select>
<input type='button' onclick='ajaxFunction()' value='add or minus one on the database for this poll id' />
</form>
</div>
</body>
</html>

pollscript.php:

<?php
require($_SERVER["DOCUMENT_ROOT"] . "/filethatconnects to mysql db.php");
$pollid = $_GET['pollid'];
$vote = $_GET['vote'];
$pollid = mysql_real_escape_string($pollid);
$vote = mysql_real_escape_string($vote);
if(is_numeric($pollid) && is_numeric($vote)){$nothing = "";$sessionid = "5";
$checkvote = mysql_num_rows(mysql_query("SELECT `$sessionid` FROM polls WHERE `$sessionid` != '$nothing' AND id = '$pollid'"));
if($checkvote == "0"){$adduser = mysql_query("ALTER TABLE polls ADD COLUMN `$sessionid` int (2) NOT NULL");
$query = "UPDATE polls SET vote = `vote`+'$vote', `$sessionid` = '$vote' WHERE id = '$pollid'";
$qry_result = mysql_query($query) or die(mysql_error());
$display_string = "thanks for letting everybody know hon.";}	
else{$display_string = "u already voted for this hon.";}}
echo $display_string;
?>

sessionid can also be like ip i guess?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.