Controling Forms with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

rafael.lucchese
Forum Newbie
Posts: 8
Joined: Fri Oct 19, 2007 7:56 am

Thanks

Post by rafael.lucchese »

Thank you for the suggestion to use links but I could not get it to work, I am definately doing something very wrong. I decided however, to move on with the buttons and I was hoping you might bew able to assist me with a problem: The the session variable that contains the textfield $_SESSION['inputText'] information does not appear to be holding the value.

Also, the operator session variable $_SESSION['operator'] does not appear to be holding a value in between times where the php code is contacted. It always prints "first case", when it should print "second case" once the $_SESSION['operator'] is initialized.

Am I making any mistakes with respect to holding a session? Could this be an issue not related to the PHP code, but the HTML, or the browser itself (IE 7)?

Here is what I have:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<?php
	session_start();
	
	if ($_SESSION['inputText'] == 0) //session variable is zero here at first
	{
		$isFirstDigit = true;
		print 'first digit';
	}
	else
	{
		$isFirstDigit = false;
		$_SESSION['inputText'] = $_POST['inputText'];
		print 'not first digit';
	}
		
	//1344.566
	// $value = explode('.','1344.566');
	// $value[0] as 1344
	// $value[1] as 566
	// $whole_number = $value[0]
	// $decimal_number = $value[1] 	
			
	if(isset($_POST['button']))
	{ 
		switch($_POST['button'])
		{
			case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9":
			{
				if($_POST['inputText'] == "" || $_POST['inputText'] == 0) 
					$_POST['inputText'] = $_POST['button'];
				 
				else
					$_POST['inputText'] .= $_POST['button'];
				
			} break;
				
			case "+": 
			{
				if(!($_SESSION['operator']))
				{	
					$_SESSION['operator'] = $_POST['button'];
					$_SESSION['firstOperand'] = $_POST['inputText'];
					$_POST['inputText'] = "";
					echo 'firstCase';
				}
				else
				{
					switch($_SESSION['operator'])
					{
						case "+" :
						{
							$_SESSION['firstOperand'] += $_POST['inputText'];
							$_SESSION['operator'] = $_POST['button'];
							$_POST['inputText'] = "";			
						}break;
						
						case "-" :
						{
							$_SESSION['firstOperand'] -= $_POST['inputText'];
							$_SESSION['operator'] = $_POST['button'];
							$_POST['inputText'] = "";
						}break;
						
						case "*" : 
						{
							$_SESSION['firstOperand'] *= $_POST['inputText'];
							$_SESSION['operator'] = $_POST['button'];
							$_POST['inputText'] = "";	
						}break;
						
						case "/" :
						{
							$_SESSION['firstOperand'] /= $_POST['inputText'];
							$_SESSION['operator'] = $_POST['button'];
							$_POST['inputText'] = "";
						}break;
					
					$inputText = $_SESSION['firstOperand'];
					
					}
					
				}
					

			}break;
			
			default : //the stuff here
		}
	
	}
	$inputText = $_POST['inputText'];
?>


<html>
	<head> 
		<title>Calculator</title> 
		<style>
			/* All style information is embeded in this file in order to produce */
			/* a single stand-alone file at the end of this project.             */
			
			.buttonSmall
			{
				width:45px; height:35px; margin:4px 0px 0px 0px;
			}

			.buttonLarge	
			{
				width:78px; height:35px;
			}

			.display
			{
				margin-bottom:10px; width:284px; text-align:right;
			}

			#container
			{
				position:absolute; height:240px; width:300px;
				background-color:#ccc; padding:20px 0px 0px 10px;
			}

			#topLeftSquare
			{
				position:relative; float:left; height:28px; width:30px;
				background-color:#ccc; margin:3px 9px 0px 9px;
			}
		</style>
	</head>

<body>

<div id="container">
<form action="phpCalc.php" method="post">

	<input type="text" name="inputText" value="<?php echo $inputText ?>" maxlength="35" class="display"><br/>
	<input type="hidden" name="inputOperator" value="<?php ?>">
	<div id="topLeftSquare"></div> <!-- Irrelevant, but present in the windows calc. -->
	
	<!-- <a href="phpCalc.php?button=MC&inputText=<?php echo $inputText; ?>" class="buttonSmall">MC</a>-->  
        
	<input type="submit" name="btnBackspace" value="Backspace" class="buttonLarge">
	<input type="submit" name="btnCe" value="CE" class="buttonLarge">
	<input type="submit" name="btnC" value="C" class="buttonLarge"><br/>
	
	<input type="submit" name="btnMc" value="MC" class="buttonSmall">
	<input type="submit" name="button" value="7" class="buttonSmall">
	
	<input type="submit" name="button" value="8" class="buttonSmall">
	<input type="submit" name="button" value="9" class="buttonSmall">
	<input type="submit" name="btnDiv" value="/" class="buttonSmall">
	<input type="submit" name="btnSquare" value="sqrt" class="buttonSmall"><br/>
	
	<input type="submit" name="btnMr" value="MR" class="buttonSmall">
	<input type="submit" name="button" value="4" class="buttonSmall">
	<input type="submit" name="button" value="5" class="buttonSmall">
	<input type="submit" name="button" value="6" class="buttonSmall">
	<input type="submit" name="btnMult" value="*" class="buttonSmall">
	<input type="submit" name="btnPercent" value="%" class="buttonSmall"><br/>
	
	<input type="submit" name="btnMs" value="MS" class="buttonSmall">
	<input type="submit" name="button" value="1" class="buttonSmall">
	<input type="submit" name="button" value="2" class="buttonSmall">
	<input type="submit" name="button" value="3" class="buttonSmall">
	<input type="submit" name="btnSub" value="-" class="buttonSmall">
	<input type="submit" name="btnFrequency" value="1/x" class="buttonSmall"><br/>
	
	<input type="submit" name="btnMplus" value="M+" class="buttonSmall">
	<input type="submit" name="button" value="0" class="buttonSmall">
	<input type="submit" name="btnSignChange" value="+/-" class="buttonSmall">
	<input type="submit" name="btnDecimal" value="." class="buttonSmall">
	<input type="submit" name="button" value="+" class="buttonSmall">
	<input type="submit" name="btnEqual" value="=" class="buttonSmall"><br/>
</div>	
</form>


</body>

</html>
Post Reply