Hello, I'm trying to write a widget that displays a poll in a side bar to interact with users better.
I came across several PHP + Ajax jQuery poll scripts and the they work okay outsite Q2A system. However, when I tried to put it in a Q2A plugin, it doesn't work as expected.
Such a script contains 3 parts: a user backend polling input & output; a javascript code that introduces an ajax page to handle the polling data when users click; and an ajax page to process data.
My question is "Do we need to register that ajax page?"
I can't find a tutorial here.
Example of a javascript script
$(document).ready(function()
{
/*
using $.post() function
$(document).on('submit', '#reg-form', function()
{
$.post('submit.php', $(this).serialize())
.done(function(data)
{
$("#reg-form").fadeOut('slow', function()
{
$(".result").fadeIn('slow', function()
{
$(".result").html(data);
});
});
})
.fail(function()
{
alert('fail to submit the data');
});
return false;
});
using $.post() function
*/
$(document).on('click', '.quickpollsubmit', function()
{
//var fn = $("#fname").val();
//var ln = $("#lname").val();
//var data = 'fname='+fn+'&lname='+ln;
//var data = $(this).serialize();
$('#quickpollsubmit').val($(this).val());
var data = $("#quickpoll").serialize();
$.ajax({
type : 'POST',
url : 'quickpoll_vote.php',
data : data,
success : function(data)
{
$("#quickpoll").fadeOut(500).hide(function()
{
$(".quickpollwrapper").fadeIn(500).show(function()
{
$(".quickpollwrapper").html(data);
});
});
}
});
return false;
});
});
Example of an ajax page (quickpoll_vote.php):
<?php
include('settings.php');
if($_POST['quickpollsubmit'] == 'Vote') {
if(($quickpolltype == "radio" && $_POST['radiovote'] != '') || ($quickpolltype == "checkbox" && $_POST['checkboxvote'] != '')){
// grab users IP address
$ipAddress = $_SERVER['REMOTE_ADDR'];
$UserIptxt = $ipAddress."||";
$all_ips = explode("||", file_get_contents("data/user_ip.txt"));
if ( ! in_array($ipAddress, $all_ips) ) {
// put the ip address in .txt file
file_put_contents("data/user_ip.txt", $UserIptxt, FILE_APPEND);
}
// ip adres is already stored
else {
echo '<div class="alreadyvoted">You have already voted!</div>';
}
}
// if no input
else {
echo '<div class="noinput">You have to make at least one choice!</div>';
}
}