minipoll-ajax

Gesponsorde koppelingen

PHP script bestanden

  1. minipoll-ajax

« Lees de omschrijving en reacties

[config.php]

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
<?php

$dbhost
= 'localhost';
$dbname = 'destruin_forum';
$dbuser = 'root';
$dbpasswd = 'flikkerop';

?>


[database]
CREATE TABLE `stelling` (
`id` int(11) NOT NULL auto_increment,
`count` int(11) NOT NULL default '0',
`titel` varchar(30) collate utf8_unicode_ci NOT NULL default '',
`vraag` varchar(90) collate utf8_unicode_ci NOT NULL default '',
`antwoord1` varchar(60) collate utf8_unicode_ci NOT NULL default '',
`antwoord2` varchar(60) collate utf8_unicode_ci NOT NULL default '',
`antwoord3` varchar(60) collate utf8_unicode_ci NOT NULL default '',
`item1` int(11) NOT NULL default '0',
`item2` int(11) NOT NULL default '0',
`item3` int(11) NOT NULL default '0',
`datum` int(11) NOT NULL default '0',
`active` varchar(6) collate utf8_unicode_ci NOT NULL default 'false',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=43 ;


CREATE TABLE `stelling_votes` (
`id` int(11) NOT NULL auto_increment,
`user_id` int(11) NOT NULL default '0',
`vraag_id` int(11) NOT NULL default '0',
`antwoord_id` int(11) NOT NULL default '0',
`datum` int(11) NOT NULL default '0',
`ip` varchar(40) collate utf8_unicode_ci NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=576 ;

[pollClass.php]
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

    function wrap($tekst, $maxlen) {
        return wordwrap(stripslashes($tekst), $maxlen, "<br />\n", true);
    }


    class minipoll {

        var
$poll;
        var
$count;
        var
$handle;
        var
$ident;

        //var $dbhost;
        var $dbname;
        //var $dbuser;
        //var $dbpasswd;

        // Init en lees record in

        function minipoll($dbhost,$dbname,$dbuser,$dbpasswd)
        {

            $handle = mysql_connect($dbhost, $dbuser, $dbpasswd);
            mysql_select_db($dbname, $handle);
              $sql = "SELECT * FROM stelling WHERE active='true'";
            $result = mysql_query($sql);
            $this->count = mysql_num_rows($result);
            $this->poll = mysql_fetch_array($result);
            $this->ident = $this->poll['id'];
            $this->handle = $handle;
            //$this->dbhost = $dbhost;
            $this->dbname = $dbname;
            //$this->dbuser = $dbuser;
            //$this->dbpasswd = $dbpasswd;


        }

        // Ophalen record voor gebruik
        function pollGet()
        {

            //echo '<pre>'.print_r($this->poll, true).'</pre>';
              return $this->poll;
        }


        // Simpele generator voor grafische weergave (lengte)
        function pollCalculate($maxlengte)
        {

            $lengte = array();
            $count = $this->pollCounter();
            if ($count > 0) {
                $lengte[1] = (($this->poll['item1'] / $count) * $maxlengte);
                $lengte[2] = (($this->poll['item2'] / $count) * $maxlengte);
                $lengte[3] = (($this->poll['item3'] / $count) * $maxlengte);
            }

            return $lengte;

        }


        // Telt totaal aantal stemmen, veld 'count' wordt niet meer gebruikt!
        function pollCounter()
        {

            return ($this->poll['item1']+$this->poll['item2']+$this->poll['item3']);
        }


        // Keuze wordt verhoogt met 1, en datum wordt geupdated
        function pollUpdate($ident, $keuze)
        {

            $datum = time();
            if ($keuze == 1) {
                $count = ++$this->poll['item1'];
                $sql = "UPDATE stelling SET item1='".$count."', datum='".$datum."' WHERE id='".$ident."'";
            }

            if ($keuze == 2) {
                $count = ++$this->poll['item2'];
                $sql = "UPDATE stelling SET item2='".$count."', datum='".$datum."' WHERE id='".$ident."'";
            }

            if ($keuze == 3) {
                $count = ++$this->poll['item3'];
                $sql = "UPDATE stelling SET item3='".$count."', datum='".$datum."' WHERE id='".$ident."'";
            }

            //$handle = mysql_connect($this->dbhost, $this->dbuser, $this->dbpasswd);
            mysql_select_db($this->dbname, $this->handle);
            $result = mysql_query($sql);
        }


        // Maak een nieuwe entry in tabel. Controle op dubbel stemmen.
        function pollUpdateVoted($ident, $user, $keuze)
        {

            $ip = $this->get_ip();
            $datum = time();
            $sql = "INSERT INTO `stelling_votes` VALUES('','".$user."','".$ident."','".$keuze."','".$datum."','".$ip."')";
            //$handle = mysql_connect($this->dbhost, $this->dbuser, $this->dbpasswd);
            mysql_select_db($this->dbname, $this->handle);
            $result = mysql_query($sql);
        }


        // Is er al een keer gestemd? Controleer op IP en vraag.
        function pollVoted($ident)
        {

            $ip = $this->get_ip();
            $sql = "SELECT id FROM stelling_votes WHERE ip='".$ip."' AND vraag_id='".$ident."'";
            //$handle = mysql_connect($this->dbhost, $this->dbuser, $this->dbpasswd);
            mysql_select_db($this->dbname, $this->handle);
            $result = mysql_query($sql);
            return mysql_num_rows($result);
        }


        // Haalt het IP-adres van de gebruiker op
        function get_ip()
        {

            if(getenv($_SERVER['HTTP_X_FORWARDED_FOR']))
            {

                   $ip = getenv($_SERVER['HTTP_X_FORWARDED_FOR']);
            }

               elseif    (getenv($_SERVER['HTTP_CLIENT_IP']))
            {

                 $ip = getenv($_SERVER['HTTP_CLIENT_IP']);
            }

               else
            {
                   $ip = $_SERVER['REMOTE_ADDR'];
            }

            return $ip;
        }

    }


?>


[pollResult.php]
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

    require_once('../stelling/pollClass.php');

    function
results() {

        include('../db/config.php');
        $stelling = new minipoll($dbhost,$dbname,$dbuser,$dbpasswd);
        $poll = $stelling->pollGet();
        // Grafische weergave berekenen
        $lengte = $stelling->pollCalculate(130);
        // Totaal gestemd (veld 'count' in tabel niet meer nodig)
        $totaal = $stelling->pollCounter();

        ?>


        <!-- Deze hoort bij de website, dit is het titelbalkje -->
        <p class="titel_menu"><?php echo stripslashes($poll['titel']); ?></p>

        <div style="margin:8px">

            <span class="pollTitel"><?php echo wrap($poll['vraag'],25); ?></span><br />

            <div style="padding-top:4px; padding-bottom:4px">

                <span class="pollTekst"><?php echo wrap($poll['antwoord1'],28); ?></span><br />
                <?php if ($lengte[1] > 0) { ?>
                    <img class="pollImage" src="/img/silver.gif" width="<?php echo $lengte[1]; ?>" alt="" border="0" />&nbsp;
                <?php } ?>
                <span class="pollCounter">(<?php echo $poll['item1']; ?>)</span><br />

                <span class="pollTekst"><?php echo wrap($poll['antwoord2'],28); ?></span><br />
                <?php if ($lengte[2] > 0) { ?>
                    <img class="pollImage" src="/img/silver.gif" width="<?php echo $lengte[2]; ?>" alt="" border="0" />&nbsp;
                <?php } ?>
                <span class="pollCounter">(<?php echo $poll['item2']; ?>)</span><br />

                <span class="pollTekst"><?php echo wrap($poll['antwoord3'],28); ?></span><br />
                <?php if ($lengte[3] > 0) { ?>
                    <img class="pollImage" src="/img/silver.gif" width="<?php echo $lengte[3]; ?>" alt="" border="0" />&nbsp;
                <?php } ?>
                <span class="pollCounter">(<?php echo $poll['item3']; ?>)</span><br />

            </div>

            <div class="pollFooter">
                (Totaal gestemd: <strong><?php echo $totaal; ?></strong>)
            </div>

        </div>

    <?php } ?>


<?php

    //echo '<pre>'.print_r($_POST, true).'</pre>'; exit;
    //if (isset($_POST['view'])) { results(); } else {

    if (isset($_POST['view'])) { results(); } else {

        include('../db/config.php');
        $stelling = new minipoll($dbhost,$dbname,$dbuser,$dbpasswd);

        // Update keuze, datum en ip controle
        $ident = mysql_real_escape_string($_POST['ident']);
        $keuze = mysql_real_escape_string($_POST['keuze']);
        $stelling->pollUpdate($ident, $keuze);
        $stelling->pollUpdateVoted($ident, 0, $keuze);
        results();
    }

?>


[pollStart.php]
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<style type="text/css">
/*<![CDATA[*/

    /*
        Declareer hier ook voor include 'pollResults',
        anders werkt het niet goed op elke browser
    */

    .radioButton {
        margin: 0;
        padding: 0;
        height: 15px;
    }
    .submitButton {
        font-size: 8pt;
    }
    .pollTitel {
        font-size: 9pt;
        font-weight: bold;
    }
    .pollTekst {
        font-size: 8pt;
        color: #505050;
    }
    .pollDatum {
        padding-top: 4px;
        font-size: 8pt;
    }
    .pollImage {
        padding-top:2px;
        height: 14px;
    }
    .pollCounter {
        font-size: 8pt;
        vertical-align: top;
    }
    .pollFooter {
        font-size: 8pt;
    }

/*]]>*/
</style>

<script type="text/javascript">
//<![CDATA[

    function POSTRequest(query)
    {
           var xmlHttp;

           if (window.ActiveXObject) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }

        var queryString = "http://" + location.hostname + "/stelling/pollResults.php?" + query;

           xmlHttp.open("POST", queryString, true);
           xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
           xmlHttp.setRequestHeader("Content-length", query.length);
           xmlHttp.setRequestHeader("Connection", "close");
           xmlHttp.send(query);

           return xmlHttp;
    }

    function doeIets(query)
    {
        var xmlHttp = POSTRequest(query);
        xmlHttp.onreadystatechange=function()
        {
           if(xmlHttp.readyState==4)
              {
              var tekst = xmlHttp.responseText;
              document.getElementById('quickPoll').innerHTML = tekst;
              }
        }
    }

    function stemmen()
    {
        var mijn_form = document.getElementById('poll');
        var query = 'ident=' + mijn_form.ident.value;
        for(var i = 0; i < mijn_form.keuze.length ; i++){
            if(mijn_form.keuze[i].checked) {
                query += '&keuze=' + mijn_form.keuze[i].value;
                doeIets(query);
                return true;
            }
        }
        alert('Je moet wel een keuze maken');
    }

    function resultaat()
    {
        var mijn_form = document.getElementById('poll');
        var query = 'ident=' + mijn_form.ident.value;
        query += '&view=true';
        doeIets(query);
        return true;
    }

//]]>
</script>


<?php

    require_once('../stelling/pollClass.php');

    function
vote($poll) {

        ?>


        <!-- Deze hoort bij de website, dit is het titelbalkje -->
        <p class="titel_menu"><?php echo stripslashes($poll['titel']); ?></p>

        <div style="margin:8px;">

            <form style="margin:0; padding:0" id="poll" action="">

                <span class="pollTitel"><?php echo wrap($poll['vraag'],25); ?></span><br />

                <div style="padding-top:4px; padding-bottom:4px">

                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td align="left" valign="top" width="20">
                            <input class="radioButton" type="radio" id="optie1" name="keuze" value="1" />
                        </td>
                        <td align="left" valign="top">
                            <label class="pollTekst" for="optie1">
                              <?php echo wrap($poll['antwoord1'],26); ?>
                            </label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" valign="top" width="20">
                              <input class="radioButton" type="radio" id="optie2" name="keuze" value="2" />
                          </td>
                          <td align="left" valign="top">
                            <label class="pollTekst" for="optie2">
                              <?php echo wrap($poll['antwoord2'],26); ?>
                            </label>
                          </td>
                    </tr>
                    <tr>
                        <td align="left" valign="top" width="20">
                              <input class="radioButton" type="radio" id="optie3" name="keuze" value="3" />
                          </td>
                          <td align="left" valign="top">
                            <label class="pollTekst" for="optie3">
                              <?php echo wrap($poll['antwoord3'],26); ?>
                            </label>
                          </td>
                    </tr>
                    </table>

                </div>

                   <input type="hidden" name="ident" value="<?php echo $poll['id']; ?>" />

            </form>

            <!-- Moet buiten form staan anders werkt het niet! -->
            <input class="submitButton" type="submit" value="Stemmen&nbsp;" onclick="javascript:stemmen()" />
            <input class="submitButton" type="submit" value="Bekijken&nbsp;" onclick="javascript:resultaat()" />

            <div class="pollDatum">
                (<?php echo strftime("%d %B %Y om %H:%M", $poll['datum']); ?>)
            </div>

        </div>

    <?php } ?>


<div id="quickPoll"> <?php

        include('../db/config.php');
        $stelling = new minipoll($dbhost,$dbname,$dbuser,$dbpasswd);
        // Indien poll actief?
        if ($stelling->count > 0) {
            $poll = $stelling->pollGet();
            // Uitzoeken welke weergave
            if ($stelling->pollVoted($stelling->ident) == 0) { vote($poll); }
                else { ?>

                    <script language="JavaScript">
                        doeIets('view=true');
                    </script>
                <?php }
        }
    ?>

</div>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.