Jump to content

php poll


vmars

Recommended Posts

HI! I am making a php poll without a database but with a txt file. I have two php scripts (one with the poll, the other with the results) and I am having trouble getting one to talk to the other. Any help is appreciated! Here they are:

 

poll.php

 

<html>
<head>
<title>POLL!</title>
<?PHP
$selected_radio = $_POST['album'];
print $selected_radio;

if (isset($_POST['Submit1'])) {
$selected_radio = $_POST['album'];
print $selected_radio;
}
?>
</head>
<body>
<h1>Cats or Dogs?</h1>
<Form name ="form1" Method ="Post" ACTION ="poll.php">

<Input type = 'Radio' Name ='album' value= 'cats'>Cats
<Input type = 'Radio' Name ='album' value= 'dogs'>Dogs
<P>
<Input type = "Submit" Name = "Submit1" Value = "Vote!">
</FORM>
<?PHP

$usea_status = 'unchecked';
$useb_status = 'unchecked';

if (isset($_POST['Submit1'])) {

$selected_radio = $_POST['album'];

if ($selected_radio == 'cats') {
$cats_status = 'checked';
}
elseif ($selected_radio == 'dogs') {
$dogs_status = 'checked';
}
}
?>
</body>
</html>

 

-----------

poll2.php

 

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0)
  {
  $yes = $yes + 1;
  }
if ($vote == 1)
  {
  $no = $no + 1;
  }

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>Result:</h2>
<table>
<tr>
<td>Cats:</td>
<td>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>Dogs:</td>
<td>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/241949-php-poll/
Share on other sites

poll2.php updated. still needing help:

 

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0)
  {
  $cats = $cats + 1;
  }
if ($vote == 1)
  {
  $dogs = $dogs + 1;
  }

//insert votes to txt file
$insertvote = $cats."||".$dogs;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>Result:</h2>
<table>
<tr>
<td>Cats:</td>
<td>
<?php echo(100*round($cats/($dogs+$cats),2)); ?>%
</td>
</tr>
<tr>
<td>Dogs:</td>
<td>
<?php echo(100*round($dogs/($dogs+$cats),2)); ?>%
</td>
</tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/241949-php-poll/#findComment-1242858
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.