1: //CMXads.com Unlimited Polls for your website noSQL
2: //Addon Utility Php Script demo tutorial example
3: //Legal to use with visible display of credit and link to cmxads.com
4: //Copyright 2021 by George CMXads.com
5:
6: //PHP INCLUDE THIS IN PAGE WHERE POLL IS RUNNING
7: <?php include('poll_content.php'); ?>
8: <?php
9: // Set your poll question and choice options
10: //to run mulitple just add as desired
11: $poll_question_1 = array( 'poll_1' => 'Do you like to take a poll?');
12:
13: $poll_options_1 =array('No. But my boy friend does.',
14: 'No. But my girl friend does.',
15: 'Yes. I take every poll I can.',
16: 'Sometimes. If I like the poll.',
17: 'Yes. I take poles in the bum.');
18: ///////////////////// END INCLUDE
19: ?>
20:
21: <?php // POLLIT.PHP The form action where you post to
22: //this script does not have a content type declared
23: // it is not included in a webpage with content type declared
24: //json must be utf-8 or can get corrupted
25: $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
26: header('Content-Type: application/json; charset=utf-8');
27: //Can run multiple polls. Just add more arrays
28: //Set your Poll question/s and options arrays
29: include('poll_content.php');
30:
31: //set error flag
32: $error='1';
33:
34: if(isset($_POST['what']) && !empty($_POST['choice']) && strlen($_POST['choice'])==1 && ctype_digit($_POST['choice'])){
35:
36:
37: $file_base =trim($_POST['what']);// The poll question array key is file name
38:
39: $choice_id = trim($_POST['choice']);//The numercic key of the selected poll option
40: //exit;
41:
42: if (array_key_exists($choice_id, $poll_options_1) && array_key_exists( $file_base, $poll_question_1)) {
43:
44:
45: $to_file = $file_base . '.db';// make file name from poll question key
46:
47: if (!file_exists($to_file) || filesize($to_file) <5){// incase a NULL in file
48: // write new choice option array to file
49: $input = array(array('choice' =>$choice_id, 'cnt'=> '1' , 'option' =>$poll_options_1[$choice_id]));
50:
51: $input= json_encode($input);
52:
53: if( file_put_contents( $to_file, $input)){
54: $error='';
55: }
56:
57: }
58: // if file exists and has entries
59: if (file_exists($to_file) && filesize($to_file) >4){
60: $f_out = file_get_contents($to_file);// read file
61: $f_out = json_decode($f_out, true);// json decode
62:
63: $found='';// initiate flag variable if we found selected option already in array
64: $i='0';// starting array key at 0
65: foreach($f_out as $opt) {//f_out array to arrays of poll options
66: // foreach loop the inner level options array
67: // each option has choice number(key), vote count and option text
68: foreach($opt as $key=>$value)
69: {
70: //if match on posted choice and array choice
71: if($key == 'choice' && $value == $choice_id){
72: // flag found set. So we know not to add a new choice option to array
73: $found='1';
74: //echo $key." ". $value. "<br>";
75: //echo $f_out[$i]['cnt']. "<br>";
76: $opt['cnt'] += '1';// get the count of that choice option and add 1
77: }
78: }
79: //in loop with $i number update $f_out array count for that choice option
80: $f_out[$i]['cnt']=$opt['cnt'];
81: $i++;
82: }
83:
84: if($found){// we updated option vote count above
85: $input= json_encode($f_out);// encode
86:
87: if( file_put_contents( $to_file, $input)){// write back to file
88: $error='';
89: }
90: }
91:
92: if(!$found){// we must add the new chosen option to array
93: // the new pushed array is the posted choice key, count=1
94: //and pull poll option text from options array
95: $input2 = array('choice' =>$choice_id, 'cnt'=> '1' , 'option' => $poll_options_1[$choice_id]);
96: array_push($f_out, $input2);// push new array into array
97: $input= json_encode($f_out);// encode
98:
99: if(file_put_contents( $to_file, $input)){// write back to file
100: $error='';
101: }
102: }
103:
104: if(!$error){
105: //echo '<html><body><h3>Thanks for taking a pole</h3></body></html>';
106: }
107: // echo '<pre>';
108: //$f_out = file_get_contents($to_file);// read and process results
109: // $f_out = json_decode($f_out, true);// json decode
110: //var_dump( $f_out);
111: // echo '</pre>';
112: //exit;
113: }
114: }
115: }
116: ?>
117:
118:
119:
120:
121: <?php //POLLRESULTS.PHP displays results when link is clicked
122: //this script does not have a content type declared
123: // it is not included in a webpage with content type declared
124: //json must be utf-8 or can get corrupted
125: header('Content-Type: application/json; charset=utf-8');
126:
127: // built in Php string filter function
128: $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
129: // if we get poll name from GET
130: if(isset($_GET['pf']) && !empty($_GET['pf'])){
131: $poll_name = trim($_GET['pf']);
132:
133: // make file name from GET
134: $poll_file = $poll_name.'.db';
135:
136: //If Poll file exists
137: if(file_exists($poll_file) && filesize($poll_file) > 5){
138:
139: $f_out = file_get_contents($poll_file);// read file
140: $f_out = json_decode($f_out, true);// decode
141:
142: //now we want to process the data to get the votes
143: //and calculate the percentages of the total votes to options
144:
145: $all_votes=0;// initialize variable
146:
147: foreach($f_out as $choice){
148: // first calculate totalvote in loop
149: $all_votes += $choice['cnt'];
150:
151: }
152:
153: if($all_votes==0)
154: {
155: print "No votes have been casted.<br>";
156:
157: }else{
158:
159: // in another loop get vote count of each option
160: foreach($f_out as $choice){
161: $percentage=($choice['cnt']/$all_votes)*100;// calculate percentage from totl votes
162: $percentage=round($percentage,2);// round off to 2 digits
163: //print results with image scaling graphic width using percentage
164: print $choice['option'] . "<br><img src='pollpic.gif' border='0' width='$percentage' height='5'>($percentage %)<br>";
165: }
166: }
167: print "<br>Total Votes Casted: $all_votes";
168: //viewed in pop up so window close script
169: echo "<br /><a href=\"javascript:window.close();\">close window</a><br>";
170: }
171: }
172: }
173: ?>
174:
175:
176: <?php
177: //THE HTML FORM
178: include('poll_content.php');
179: if (isset($poll_question_1['poll_1'])){
180: ?>
181: <html>
182: <head>
183: <script type="text/javascript">
184: function submitTwice(f){
185: document.getElementById('poll').innerHTML='Thanks for taking a pole';
186: f.action = 'pollit.php';
187: f.target='p_result';
188: f.submit();
189: }
190: </script>
191:
192: <script type="text/javascript">
193: <!-- Begin
194: function popUp(URL) {
195: day = new Date();
196: id = day.getTime();
197: eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=250,height=280');");
198: }
199: // End -->
200: </script>
201:
202: <style type="text/css">
203: input[type="radio"] {
204: display: none;
205: }
206:
207: input[type="radio"] + div {
208: height: 28px;
209: width: 30px;
210: display: inline-block;
211: cursor: pointer;
212: vertical-align: middle;
213: background: #eee;
214: border: 1px solid #000;
215: border-radius: 100%;
216: }
217:
218: input[type="radio"] + div:hover {
219: border-color: #c2c2c2;
220: }
221:
222: input[type="radio"]:checked + div {
223: background:gray;
224: background-image:url(img08-M.jpg);
225: }
226: </style>
227:
228: </head>
229: <body>
230:
231: <?php
232: echo "<div class ='poll'>";
233: echo "<iframe style=\"position: absolute; width:0; height:0; border:0;\" name =\"p_result\" id=\"p_result\"></iframe>";
234: echo "<div style=\"font-size:18px;color:darkgreen;\" name =\"poll\" id=\"poll\"></div>";
235: print "<form name='form' method='post'>";
236: echo '<input type="hidden" name="test" value="test post data" />';
237: foreach ($poll_question_1 as $poll_file => $poll_question) {
238: echo "<h3>$poll_question</h3>";
239: foreach ($poll_options_1 as $quest_id => $poll_question) {
240:
241: print "<label><input type='radio' name='choice' value='$quest_id' />$poll_question<div></div></label><br><br>";
242: }
243: echo "<input type=\"hidden\" name=\"what\" value=\"{$poll_file}\" />";
244: print '<input type="button" name="Submit" value="Submit" onClick=submitTwice(this.form);document.getElementById("p_result").style.display="block">';
245: // print "<input type='submit' name='submit' value='vote'></form><br>";
246: print "<p><a href=\"javascript:popUp('pollresults.php?pf=$poll_file')\"><b>Poll Results</b></a></p>";
247: echo "<p style='font-size:0.7em;'><a href='https://cmxads.com/' title='this link must remain intact and visible for legal usage. visit cmxads.com for more scripts tips and advertising services'>© 2022 CMXads.com Unlimited polls script.</a></p>";
248: }
249: print "</div></body></html>";
250: }
251: ?>
252:
253: