Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 28th, 2014, 07:29 AM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question Trying to make a simple Poll program

I'm trying to create a program that will ask the user to choose between two questions, do calculations on the results, then display those results to the user. For some reason my program is not working . Please help me with my problem. My code is below:

Code:
<?php
$answerYes = null;
$answerNo = null;
 global $answer; 
 if (isset($_POST['submit'])){
 $answer = $_POST['answer'];
 echo $answer;
  if ($answer == 'Yes'){
$answerYes = file_get_contents('answerYes.txt') or die('ERROR: Cannot find file');
$answerYesTwo = (integer) $answerYes;
++$answerYesTwo;
file_put_contents('answerYes.txt',(string)$answerYesTwo) or die ('ERROR: Cannot write file');
echo "<p =\"choice\">Your choice was submitted</p>";
}
else if ($answer == 'No'){
$answerNo = file_get_contents('answerNo.txt') or die('ERROR: Cannot find file');
$answerNo = (integer) $answerNo;
++$answerNoTwo;
file_put_contents('answerNo.txt',(string)$answerNoTwo) or die ('ERROR: Cannot write file');
echo "<p =\"choice\">Your choice was submitted</p>";
}
 }
if (!isset($_POST['submit'])){
echo "<form action=\"Test.php\" method=\"post\">";
echo "<input type=\"radio\" name=\"answer\" value=\"Yes\">Yes</input>";
echo "<input type=\"radio\" name=\"answer\" value=\"No\">No</input><br>";
echo "<button type=\"submit\">Submit</button>";
echo "</form>";
}
Thanks,
Truck35
 
Old September 24th, 2014, 06:43 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Here you go;
Code:
<?php

$counter = 0;

if (isset($_POST['submit']))
{
	$choice = ( isset($_POST['answer']) ? $_POST['answer'] : '');
	if( !empty($choice) )
	{
		echo 'You selected: ' . $choice . '<br />';
	}
	else
	{
		echo 'No selection made, you must choose either \'Yes\' or \'No\'.<br />';
	}
	
	if ($choice == 'Yes')
	{
		$yes_count = file_get_contents('./answerYes.txt');
		if( $yes_count !== false )
		{
			$counter = (int) $yes_count;
			++$counter;
			file_put_contents('answerYes.txt',(string)$counter) or die ('ERROR: Cannot write file');
			echo '<p class="choice">Your choice was submitted</p>';
		}
		else
		{
			die('ERROR: Cannot find file');
		}
	}
	elseif ($choice == 'No')
	{
		$no_count = file_get_contents('./answerNo.txt');
		if( $no_count !== false )
		{
			$counter = (int) $no_count;
			++$counter;
			file_put_contents('answerNo.txt',(string)$counter) or die ('ERROR: Cannot write file');
			echo '<p class="choice">Your choice was submitted</p>';
		}
		else
		{
			die('ERROR: Cannot find file');
		}
	}
}

if (!isset($_POST['submit']) || !isset($_POST['answer']) )
{
	echo '<form action="_btest.php" method="post">';
	echo '<input type="radio" name="answer" value="Yes" />Yes';
	echo '&nbsp;&nbsp;&nbsp;';
	echo '<input type="radio" name="answer" value="No" />No<br />';
	echo '<button type="submit" name="submit">Submit</button>';
	echo '</form>';
}
?>
The Following User Says Thank You to UseLess For This Useful Post:
Truck35 (September 27th, 2014)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help!! Example of a simple program used for business! paulymonz Java GUI 1 April 23rd, 2012 03:51 AM
Can you make me a Program?? benhar03 VB Databases Basics 3 February 11th, 2008 10:34 PM
Simple Program Help hobbit666 Visual Basic 2005 Basics 2 December 19th, 2006 11:51 AM
Simple Java Program WargeMaster Java GUI 1 September 9th, 2005 12:25 PM
i need to make this program saudyonline General .NET 11 October 27th, 2004 12:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.