sätt någon klass, id på den länken och bind click eventet till ajax anropet så få du ju vyn tillbaka, där kan du ju leta upp "tillbaka poll" blocket och ersätt tillbaka den.
Håller på med denna:
http://webdeveloperplus.com/php/ajax-user-poll-using-jquery-and-php/
och nu vill jag på något sätt kunna få en Gå tillbaka till Röstningen om jag klickat på View results för att kolla resultatet. Alltså utan att jag röstat.
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>AJAX Poll Using jQuery and PHP</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript" src="js/jquery-1.3.2.js" ></script>
<script type="text/javascript" >
$(function(){
var loader=$('#loader');
var pollcontainer=$('#pollcontainer');
loader.fadeIn();
//Load the poll form
$.get('poll.php', '', function(data, status){
pollcontainer.html(data);
animateResults(pollcontainer);
pollcontainer.find('#viewresult').click(function(){
//if user wants to see result
loader.fadeIn();
$.get('poll.php', 'result=1', function(data,status){
pollcontainer.fadeOut(1000, function(){
$(this).html(data);
animateResults(this);
});
loader.fadeOut();
});
//prevent default behavior
return false;
}).end()
.find('#pollform').submit(function(){
var selected_val=$(this).find('input[name=poll]:checked').val();
if(selected_val!=''){
//post data only if a value is selected
loader.fadeIn();
$.post('poll.php', $(this).serialize(), function(data, status){
$('#formcontainer').fadeOut(100, function(){
$(this).html(data);
animateResults(this);
loader.fadeOut();
});
});
}
//prevent form default behavior
return false;
});
loader.fadeOut();
});
function animateResults(data){
$(data).find('.bar').hide().end().fadeIn('slow', function(){
$(this).find('.bar').each(function(){
var bar_width=$(this).css('width');
$(this).css('width', '0').animate({ width: bar_width }, 1000);
});
});
}
});
</script>
</head>
<body>
<div id="container" >
<h1>User Poll</h1>
<div id="pollcontainer" >
</div>
<p id="loader" >Loading...</p>
</div>
</body>
</html>
poll.sql
CREATE TABLE IF NOT EXISTS `options` (
`id` int(11) NOT NULL auto_increment,
`ques_id` int(11) NOT NULL,
`value` varchar(300) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
INSERT INTO `options` (`id`, `ques_id`, `value`) VALUES
(1, 1, 'Bit.ly'),
(2, 1, 'TinyURL'),
(3, 1, 'is.gd'),
(4, 1, 'tr.im'),
(5, 1, 'Other');
CREATE TABLE IF NOT EXISTS `questions` (
`id` int(11) NOT NULL auto_increment,
`ques` text NOT NULL,
`created_on` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
INSERT INTO `questions` (`id`, `ques`, `created_on`) VALUES
(1, 'Which URL Shortening service you use most?', '2009-10-13 07:42:18');
CREATE TABLE IF NOT EXISTS `votes` (
`id` int(11) NOT NULL auto_increment,
`option_id` int(11) NOT NULL,
`voted_on` datetime NOT NULL,
`ip` varchar(16) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
poll.php
<?php
//Update database information according to your server settings
$conn=mysql_connect('localhost', 'root', 'sikhadmin') or die("Can't connect to mysql host");
//Select the database to use
mysql_select_db('polls') or die("Can't connect to DB");
if(!$_POST['poll'] || !$_POST['pollid']){
$query=mysql_query("SELECT id, ques FROM questions ORDER BY id DESC LIMIT 1");
while($row=mysql_fetch_assoc($query)){
//display question
echo "<p class=\"pollques\" >".$row['ques']."</p>";
$poll_id=$row['id'];
}
if($_GET["result"]==1 || $_COOKIE["voted".$poll_id]=='yes'){
//if already voted or asked for result
showresults($poll_id);
exit;
}
else{
//display options with radio buttons
$query=mysql_query("SELECT id, value FROM options WHERE ques_id=$poll_id");
if(mysql_num_rows($query)){
echo '<div id="formcontainer" ><form method="post" id="pollform" action="'.$_SERVER['PHP_SELF'].'" >';
echo '<input type="hidden" name="pollid" value="'.$poll_id.'" />';
while($row=mysql_fetch_assoc($query)){
echo '<p><input type="radio" name="poll" value="'.$row['id'].'" id="option-'.$row['id'].'" />
<label for="option-'.$row['id'].'" >'.$row['value'].'</label></p>';
}
echo '<p><input type="submit" value="Submit" /></p></form>';
echo '<p><a href="'.$_SERVER['PHP_SELF'].'?result=1" id="viewresult">View result</a></p></div>';
}
}
}
else{
if($_COOKIE["voted".$_POST['pollid']]!='yes'){
//Check if selected option value is there in database?
$query=mysql_query("SELECT * FROM options WHERE id='".intval($_POST["poll"])."'");
if(mysql_num_rows($query)){
$query="INSERT INTO votes(option_id, voted_on, ip) VALUES('".$_POST["poll"]."', '".date('Y-m-d H:i:s')."', '".$_SERVER['REMOTE_ADDR']."')";
if(mysql_query($query))
{
//Vote added to database
setcookie("voted".$_POST['pollid'], 'yes', time()+86400*300);
}
else
echo "There was some error processing the query: ".mysql_error();
}
}
showresults(intval($_POST['pollid']));
}
function showresults($poll_id){
global $conn;
$query=mysql_query("SELECT COUNT(*) as totalvotes FROM votes WHERE option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')");
while($row=mysql_fetch_assoc($query))
$total=$row['totalvotes'];
$query=mysql_query("SELECT options.id, options.value, COUNT(*) as votes FROM votes, options WHERE votes.option_id=options.id AND votes.option_id IN(SELECT id FROM options WHERE ques_id='$poll_id') GROUP BY votes.option_id");
while($row=mysql_fetch_assoc($query)){
$percent=round(($row['votes']*100)/$total);
echo '<div class="option" ><p>'.$row['value'].' (<em>'.$percent.'%, '.$row['votes'].' votes</em>)</p>';
echo '<div class="bar ';
if($_POST['poll']==$row['id']) echo ' yourvote';
echo '" style="width: '.$percent.'%; " ></div></div>';
}
echo '<p>Total Votes: '.$total.'</p>';
}
Vill alltså lägga till typ
echo '<a href="'.$_SERVER['PHP_SELF'].'" id="???">Back to vote</a></p>';
efter echo '<p>Total Votes: '.$total.'</p>'; i poll.php
Någon som vet hur jag skall skriva då i javascriptkoden samt i den länken?

