E-Siber.com
teknoloji haberinin
değil, bilgisinin
peşinde...
   

"WEB"in Son Çıkan Önemli Yazıları

 

Sitede şu an 1546 yazı bulunmaktadır.

Son Yorumlar

PHP, MySQL ve jQuery ile Ajax Anket Uygulaması

Tanıtacağımız ve anlatacağımız WebResourcesDepot uygulaması, PHP, MySQL ve jQuery ile yapılmış, anket sonuçlarını renkli ve hareketli çubuklar halinde gösteren harika bir Ajaxlı anket scriptidir. Script çok basit bir mantıkla çalışıyor ve web sayfalarına eklenmesi de çok kolay. Anketi eklemek için getPoll() şeklinde fonksiyonu çağırmak yetiyor.

Ajax Poll Script

Uygulamanın kod kısmı 3 parçadan oluşuyor: HTML, Javascript (jQuery) ve PHP kodları.

 

HTML Kısmı

<div id="pollWrap">
    <form name="pollForm" method="post" action="inc/functions.php?action=vote">
        <h3>Poll Question 1</h3>
        <ul>
            <li>
                <input name="pollAnswerID" id="pollRadioButton1" value="1" type="radio">
                Answer1 for Poll1
                <span id="pollAnswer1"></span>
            </li>
            <li class="pollChart pollChart1"></li>
            <li>
                <input name="pollAnswerID" id="pollRadioButton2" value="2" type="radio">
                Answer2 for Poll1
                <span id="pollAnswer2"></span>
            </li>
            <li class="pollChart pollChart2"></li>
        </ul>
        <input name="pollSubmit" id="pollSubmit" value="Vote" type="submit">
        <span id="pollMessage"></span>
        <img src="ajaxLoader.gif" alt="Ajax Loader" id="pollAjaxLoader">
    </form>
</div>

 Anketteki her bir seçim için farklı bir ID ayarlanıyor. Ve herbiri de veritabanındaki kendi IDleri ile eştir.

 

PHP Kısmı

Önce veritabanına bağlanıyor:

require("db.php");
//GETTING VARIABLES START
$action         = mysql_real_escape_string($_POST['action']);
$pollAnswerID    = mysql_real_escape_string($_POST['pollAnswerID']);
//GETTING VARIABLES END

Ardından, veritabanında döngü yapan be yukarıdaki anketin HTML'sini oluşturan getPoll fonksiyonu oluşturulur:

function getPoll($pollID){
    $query  = "SELECT * FROM polls LEFT JOIN pollAnswers ON polls.pollID = pollAnswers.pollID WHERE polls.pollID = " . $pollID . " ORDER By pollAnswerListing ASC";
    $result = mysql_query($query);
    //echo $query;jquery

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        $pollQuestion         = $row['pollQuestion'];
        $pollAnswerID         = $row['pollAnswerID'];
        $pollAnswerValue    = $row['pollAnswerValue'];

        if ($pollStartHtml == '') {
            $pollStartHtml     = '<div id="pollWrap"><form name="pollForm" method="post" action="inc/functions.php?action=vote"><h3>' . $pollQuestion .'</h3><ul>';
            $pollEndHtml     = '</ul><input type="submit" name="pollSubmit" id="pollSubmit" value="Vote" /> <span id="pollMessage"></span><img src="ajaxLoader.gif" alt="Ajax Loader" id="pollAjaxLoader" /></form></div>';
        }
        $pollAnswersHtml    = $pollAnswersHtml . '<li><input name="pollAnswerID" id="pollRadioButton' . $pollAnswerID . '" type="radio" value="' . $pollAnswerID . '" /> ' . $pollAnswerValue .'<span id="pollAnswer' . $pollAnswerID . '"></span></li>';
        $pollAnswersHtml    = $pollAnswersHtml . '<li class="pollChart pollChart' . $pollAnswerID . '"></li>';
    }
    echo $pollStartHtml . $pollAnswersHtml . $pollEndHtml;
}

İlgili anketi için answerID'yi sağlayan getPollID fonksiyonu:

function getPollID($pollAnswerID){
    $query  = "SELECT pollID FROM pollAnswers WHERE pollAnswerID = ".$pollAnswerID." LIMIT 1";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);

    return $row['pollID'];
}

Ve getPollResults fonksiyonu:

function getPollResults($pollID){
    $colorArray = array(1 => "#ffcc00", "#00ff00", "#cc0000", "#0066cc", "#ff0099", "#ffcc00", "#00ff00", "#cc0000", "#0066cc", "#ff0099");
    $colorCounter = 1;
    $query  = "SELECT pollAnswerID, pollAnswerPoints FROM pollAnswers WHERE pollID = ".$pollID."";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
        if ($pollResults == "") {
            $pollResults = $row['pollAnswerID'] . "|" . $row['pollAnswerPoints'] . "|" . $colorArray[$colorCounter];
        } else {
            $pollResults = $pollResults . "-" . $row['pollAnswerID'] . "|" . $row['pollAnswerPoints'] . "|" . $colorArray[$colorCounter];
        }
        $colorCounter = $colorCounter + 1;
    }
    $query  = "SELECT SUM(pollAnswerPoints) FROM pollAnswers WHERE pollID = ".$pollID."";
    $result = mysql_query($query);
    $row = mysql_fetch_array( $result );
    $pollResults = $pollResults . "-" . $row['SUM(pollAnswerPoints)'];
    echo $pollResults;
}

Ve son oylama ve çerez işini yapan PHP kodu:

if ($action == "vote"){

    if (isset($_COOKIE["poll" . getPollID($pollAnswerID)])) {
        echo "voted";
    } else {
        $query  = "UPDATE pollAnswers SET pollAnswerPoints = pollAnswerPoints + 1 WHERE pollAnswerID = ".$pollAnswerID."";
        mysql_query($query) or die('Error, insert query failed');
        setcookie("poll" . getPollID($pollAnswerID), 1, time()+259200, "/", ".webresourcesdepot.com");
        getPollResults(1);
    }
}

 

jQuery(Javascript) Kısmı

Seçili cevabı php koduna yapıştıran Ajax kodu:

$(document).ready(function() {   

    $("#pollAjaxLoader").hide(); //hide the ajax loader
    $("#pollMessage").hide(); //hide the ajax loader
    $("#pollSubmit").click(function() {
        var pollAnswerVal = $('input:radio[name=pollAnswerID]:checked').val();//Getting the value of a selected radio element.
        if ($('input:radio[name=pollAnswerID]:checked').length) {
            $("#pollAjaxLoader").show(); //show the ajax loader
            $.ajax({
                type: "POST",
                url: "inc/functions.php",
                data: { pollAnswerID: pollAnswerVal, action: "vote" },
                success: function(theResponse) {
                    //the functions.php returns a response like "1|13|#ffcc00-2|32|#00ff00-3|18|#cc0000-63" which the first number is the answerID, second is the points it has and third is the color for that answer's graph. The last number is the sum of all points for easilt calculating percentages.
                    if (theResponse == "voted") {
                        $("#pollAjaxLoader").hide(); //hide the ajax loader
                        $("#pollMessage").html("sorry, you already voted.").fadeTo("slow", 1);
                    } else {
                        var numberOfAnswers         = (theResponse).split("-").length-2;//calculate the number of answers
                        var splittedResponse         = (theResponse).split("-");
                        var pollAnswerTotalPoints     = splittedResponse[numberOfAnswers+1];

                        for (i=0;i<=numberOfAnswers;i++)
                        {
                            var splittedAnswer         = (splittedResponse[i]).split("|");
                            var pollAnswerID         = (splittedAnswer[0]);
                            var pollAnswerPoints     = (splittedAnswer[1]);
                            var pollAnswerColor     = (splittedAnswer[2]);
                            var pollPercentage        = (100 * pollAnswerPoints / pollAnswerTotalPoints);
                            $(".pollChart" + pollAnswerID).css("background-color",pollAnswerColor);
                            $(".pollChart" + pollAnswerID).animate({width:pollPercentage + "%"});
                            $("#pollAnswer" + pollAnswerID).html(" (" + Math.round(pollPercentage) + "% - " + pollAnswerPoints + " votes)");
                            $("#pollRadioButton" + pollAnswerID).attr("disabled", "disabled"); //disable the radio buttons
                        }
                        $("#pollAjaxLoader").hide(); //hide the ajax loader again
                        $("#pollSubmit").attr("disabled", "disabled"); //disable the submit button
                    }
                }
            });
            return false;

        } else {
            $("#pollMessage").html("please select an answer.").fadeTo("slow", 1, function(){
                setTimeout(function() {
                    $("#pollMessage").fadeOut("slow");
                }, 3000);
            });
            return false;
        }

    });

});

Uygulamanın orjinal adresini ziyaret ederek daha detaylı bilgilere ulaşabilirsiniz.

Kaynak: http://www.webresourcesdepot.com/ajax-poll-script-with-php-mysql-jquery/
Demo: http://webresourcesdepot.com/wp-content/uploads/file/ajax-poll-script/
İndir: http://www.webresourcesdepot.com/?download=AjaxPollScript


· · · · · · · · · ·
Yazan: | 24.06.2010 | 4829 kez okundu.

 


WEB

Yukarı Çık

© 2012 E-SİBER BİLGİ-İLETİŞİM TEKNOLOJİLERİ | Hakkımızda
Bilişim | İnternet | Bilgi Güvenliği | Sosyal Medya | Teknoloji | M. Mekin Pesen
Adresler: E-Siber.com | E-Siber.net | ESiber.com | RSS | Facebook | Twitter | Abone Olun
IP: 38.107.179.220 | Süre: 0.661 saniye. | İletişim | Reklam Verin | Politikalar | İstatistikler