Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some Help Posting Votes on my Page

Status
Not open for further replies.

Cputerace

Technical User
Nov 27, 2000
2
US
I have a page with funny pictures on it, i want to know how i can let users vote yes/no if they like it.

any help would be appreciated

MikeBinns@mail.com
 
Hello Cputerace,

Sorry to take so long to respond - been a little busy..... The following is a piece of CGI that builds a voting page, maintains a text file containing vote counts, and displays the current vote totals. It is bare-bones, but I hope it will illustrate one approach to doing what you are describing. It uses a text file, 'votes.txt' in the same dir as the CGI code which must have the permissions set appropriately for the httpd daemon to be able to write to the file. The file structure is simple....
Code:
pic1|0
pic2|0
pic3|0

I did not make the example smart enough to build the file the first time, so you need to write 'votes.txt' as listed above for the code to work.

Here's the code.... I hope it helps.....
Code:
#!/usr/local/bin/perl
use CGI;

$query = new CGI;
$thisCGI = $query->url();

print $query->header,
    $query->start_html(-title=>"VOTE for a PICTURE");
print $query ->start_multipart_form('POST',"$thisCGI");

# read existing vote totals and build a hash
open(FILE,&quot;<votes.txt&quot;) or 
    &showError(&quot;Failed to open FILE, $!&quot;);
while ($line = <FILE>)
  {
  chomp($line); # remove line terminators
  ($pic,$num) = split(/\|/,$line);  # split on the pipe
  $vote{$pic}= $num; # build the hash named 'vote'
  }
close FILE;

print <<EndPrint;
<table>
<tr>
  <td><IMG SRC='firstIMG' ALT='put first image here'></td>
  <td><IMG SRC='secondIMG' ALT='put second image here'></td>
  <td><IMG SRC='thirdIMG' ALT='put third image here'></td>
</tr>
<tr>
  <td><A HREF=&quot;$thisCGI?pic=1&quot;>vote for 1</a></td>
  <td><A HREF=&quot;$thisCGI?pic=2&quot;>vote for 2</a></td>
  <td><A HREF=&quot;$thisCGI?pic=3&quot;>vote for 3</a></td>
</tr>
</table>
EndPrint


if ($pic_num = $query->param('pic'))
  {
  open(OPF,&quot;>votes.txt&quot;) or
      &showError(&quot;Failed to open votes.txt for writing, $!&quot;);
  foreach $key (keys(%vote))
      {
      # increment number for picture that received a vote
      if ($key =~ /pic$pic_num/) { $vote{$key}++; }
      # write new totals
      print OPF &quot;$key|$vote{$key}\n&quot;;
    }
  close OPF;
  }
print '<P>Current Totals:<BR>';
foreach $key (keys(%vote)) 
 { print &quot;pic $key num $vote{$key}<BR>\n&quot;; }
print '</P><BR>';

print $query->end_form;
print $query->end_html;

####################################################
sub showError
{
my @error = @_;
print &quot;<CENTER><font color=\&quot;\#ff4500\&quot;>ERROR - @error</font><BR>\n&quot;;
print &quot;Submission aborted - your data was not saved!!<BR>\n&quot;;
print &quot;<BR></CENTER>\n&quot;;
print $query->end_form,$query->end_html;
exit;
}

Feel free to ask for clarifications, if needed.
Good Luck.




keep the rudder amid ship and beware the odd typo
 
Thanks, i guess i dont really understand cgi enough to understand what you wrote. i got this script from someone offering a free voting booth that uses their cgi bin, how could i modify this string so it would use my own cgi bin and what files would i need in my cgi bin to get this to work?

<form method=&quot;post&quot;
action=&quot; <input type=&quot;hidden&quot; name=fcn value=add>
<input type=&quot;hidden&quot; name=ballotname value=&quot;myvote1001&quot;>
<input type=&quot;hidden&quot; name=next_url value= <input type=&quot;hidden&quot; name=question
value=&quot;Do you like my web page?&quot;>
Do you like my web page?<br>
<select name=&quot;answer&quot;>
<option>Yes
<option>No
</select>
<input type=&quot;submit&quot; value=&quot;Vote&quot;>
</form>

( is their page)


Thanks

Mike Binns
 
OK - sorry if I shot to high. We'll throttle back a little bit.

First, I would suggest taking a look at which is part 1 of 3 in a series about Perl basics.

Second, a little about CGI......
You need two chunks of stuff to do the typical CGI trick. The first chunk is just a link or input/submit to fire a piece of CGI code on a web server. The second chunk is the CGI code itself. The stuff you listed in the previous post is the first chunk. As written, the '<FORM action=.....>' tag points a piece of code (vote.pl) on ' in their cgi-bin path. To make it work on your system, simply change
the action to point at your server/path/vote.pl. That having been said (typed), you will neeed to get a copy of the code (vote.pl) and put it on your server in the place that you pointed to in your action tag. I would not do this blindly. There are some real security issues in writting CGI stuff. You need to be able to look at what you get to insure that there are no gaping holes in it. You can see some good security FAQs on The code I listed previously makes use of the CGI module that does some security stuff for you. After that, I can't advise how to set up the code on your system with out seeing it. There might be paths or other system specific items that would need to be tweaked.

I don't think it is advisable to just borrow someone's code (even with their permission) and use it with out gaining some real understanding of what you are doing. You might set yourself up for some real head aches. Again, has a number of how-to's that would be useful.

If you are interested, I can do a better job of explaining the code I posted.

I hope this helps....




keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top