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

Controling Forms with PHP

Post by rafael.lucchese »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello, 

I am very new to PHP and am currently trying to emulate the windows calculator. I am trying to understand the notion of how to get a form button to communicate with PHP. For example, if a user presses the "5" button, I would like 5 to be appended onto the text field/display (if it is not the first digit, at which case the initial 0 that sits on the screen needs to be clared first). Here is what I have so far, the interface is built, and I have been trying to use the $_POST array then echo, but I cannot get the display value to change.

Thanks in advance,

Rafael.


[syntax="php"]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php
	$inputText = 0;
	$isFirstInput = true; // Determines if the number being pressed is the first
	//in the display ("0." has to be replaced with the number plus ".")
	$isFirstOperand = true; // Determines if the operand being stored is the one
	//to the left side of the operator
	$operator = ""; // Will store the name of the operator upon onRelease of a #btn
	$reg1 = 0; // Stores a value in the memory. It will be a number
	$operand1 = 0; //Stores first value in an operation statement
	$operand2 = 0; //Stores the second value in the operation
	
	//$formatted = number_format($number, 2);

//During the second operation, the program needs to be aware that a first
//operand and an operator already exist
function secondOperand($isFirstOperand) 
{
	$isFirstOperand = false; // Defines that the number is the second operand	
	return $isFirstOperand;
}

function doOperation($operand1, $operand2, $operator)
{
	if(is_numeric($inputText)) /* */
	{
		if($operator != null) /* Operator info stored here */
		{
			switch($operator)
			{
				case "btnAdd" : $result= $operand1 + $operand2; break;
				case "btnSub" : $result= $operand1 - $operand2; break;
				case "btnMult" : $result= $operand1 * $operand2; break;
				case "btnDiv" : $result= $operand1 / $operand2; break;
			}
	
			echo("Calculation result: $result");
			//For testing only, this needs to output to the text box
		}
	}
	else
	{ 
		echo("Invalid entry - please retry with numbers only.");
	}
}

?>


<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="$_SERVER['PHP_SELF']" method="post">
	<input type="text" name="inputText" value="<?php echo $inputText; ?>" maxlength="35" class="display"><br/>
	<div id="topLeftSquare"></div> <!-- Irrelevant, but present in the windows calc. -->
	
	<input type="button" name="btnBackspace" value="Backspace" class="buttonLarge">
	<input type="button" name="btnCe" value="CE" class="buttonLarge">
	<input type="button" name="btnC" value="C" class="buttonLarge"><br/>
	
	<input type="button" name="btnMc" value="MC" class="buttonSmall">
	<input type="button" name="btnSeven" value="7" class="buttonSmall"
	onClick="<?php $_POST['inputText']= "7"; echo $_POST['inputText']; ?>">
	<input type="button" name="btnEight" value="8" class="buttonSmall">
	<input type="button" name="btnNine" value="9" class="buttonSmall">
	<input type="button" name="btnDiv" value="/" class="buttonSmall">
	<input type="button" name="btnSquare" value="sqrt" class="buttonSmall"><br/>
	
	<input type="button" name="btnMr" value="MR" class="buttonSmall">
	<input type="button" name="btnFour" value="4" class="buttonSmall">
	<input type="button" name="btnFive" value="5" class="buttonSmall">
	<input type="button" name="btnSix" value="6" class="buttonSmall">
	<input type="button" name="btnMult" value="*" class="buttonSmall">
	<input type="button" name="btnPercent" value="%" class="buttonSmall"><br/>
	
	<input type="button" name="btnMs" value="MS" class="buttonSmall">
	<input type="button" name="btnOne" value="1" class="buttonSmall">
	<input type="button" name="btnTwo" value="2" class="buttonSmall">
	<input type="button" name="btnThree" value="3" class="buttonSmall">
	<input type="button" name="btnSub" value="-" class="buttonSmall">
	<input type="button" name="btnFrequency" value="1/x" class="buttonSmall"><br/>
	
	<input type="button" name="btnMplus" value="M+" class="buttonSmall">
	<input type="button" name="btnZero" value="0" class="buttonSmall">
	<input type="button" name="btnSignChange" value="+/-" class="buttonSmall">
	<input type="button" name="btnDecimal" value="." class="buttonSmall">
	<input type="button" name="btnAdd" value="+" class="buttonSmall">
	<input type="button" name="btnEqual" value="=" class="buttonSmall"><br/>
</div>	
</form>


</body>

</html>

Everah | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

It would be much more beneficial for you to use Javascript, because in every time the user presses a button the page has to go to the server, parse the php, then send the new page back. So definately use JS to achieve this method.

This is the first one I can find - but the method is the same:
http://codepunk.hardwar.org.uk/ajs24.htm

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

Thank you

Post by rafael.lucchese »

Greg,

Thanks, I think that you have quite a point there :) Javascript would be a great way to approach this problem. Unfortunately, it is a requirement for this assignment that I would get it done with PHP, so that is why I have been sitting in front of my code for a few hours trying to figure it out. Do you have any insights on how you would approach this problem it you had to do it in PHP, or maybe if you just had to make the buttons output a number to the text display box?

Thanks.

Best regards,

Rafael.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

PHP = Server side.
Javascript = Client side.

What you are doing is interacting with the client. When a button is pushed (without Javascript) typically it sends the current form data to another page the server referenced by the form "action" tag.

In order to have a value of a form button appear in a text box, you will have to A) use Javascript on the client or B) Post it to the server and let the server side app process and output for you.
rafael.lucchese
Forum Newbie
Posts: 8
Joined: Fri Oct 19, 2007 7:56 am

PHP

Post by rafael.lucchese »

Thank you for your reply and the code correction as well,


Does this button have to be a submit button? Should I make every button in the calculator a submit button?

Thanks,

Rafael.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try using <input type="submit"> and <input type="button"> and see what the differences are.
rafael.lucchese
Forum Newbie
Posts: 8
Joined: Fri Oct 19, 2007 7:56 am

Post by rafael.lucchese »

Thanks. Here is what I have done after using the submit input type. I am not able to update the value but I have a feeling that I am very close. Let me know if you can offer me any more advice.

Thanks again for your help.

Rafael.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php
	$inputText = $_POST['inputText'];
	$isFirstInput = true; // Determines if the number being pressed is the first
	//in the display ("0." has to be replaced with the number plus ".")
	$isFirstOperand = true; // Determines if the operand being stored is the one
	//to the left side of the operator
	$operator = ""; // Will store the name of the operator upon onRelease of a #btn
	$reg1 = 0; // Stores a value in the memory. It will be a number
	$operand1 = 0; //Stores first value in an operation statement
	$operand2 = 0; //Stores the second value in the operation
	
	//$formatted = number_format($number, 2);

//During the second operation, the program needs to be aware that a first
//operand and an operator already exist
function secondOperand($isFirstOperand) 
{
	$isFirstOperand = false; // Defines that the number is the second operand	
	return $isFirstOperand;
}

function doOperation($operand1, $operand2, $operator)
{
	if(is_numeric($inputText)) /* */
	{
		if($operator != null) /* Operator info stored here */
		{
			switch($operator)
			{
				case "btnAdd" : $result= $operand1 + $operand2; break;
				case "btnSub" : $result= $operand1 - $operand2; break;
				case "btnMult" : $result= $operand1 * $operand2; break;
				case "btnDiv" : $result= $operand1 / $operand2; break;
			}
	
			echo("Calculation result: $result");
			//For testing only, this needs to output to the text box
		}
	}
	else
	{ 
		echo("Invalid entry - please retry with numbers only.");
	}
}

?>


<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/>
	<div id="topLeftSquare"></div> <!-- Irrelevant, but present in the windows calc. -->
	
	<input type="button" name="btnBackspace" value="Backspace" class="buttonLarge">
	<input type="button" name="btnCe" value="CE" class="buttonLarge">
	<input type="button" name="btnC" value="C" class="buttonLarge"><br/>
	
	<input type="button" name="btnMc" value="MC" class="buttonSmall">
	<input type="submit" name="btnSeven" value="7" class="buttonSmall"
	onClick="<?php $_POST['inputText']= '7'; ?>">
	<input type="button" name="btnEight" value="8" class="buttonSmall">
	<input type="button" name="btnNine" value="9" class="buttonSmall">
	<input type="button" name="btnDiv" value="/" class="buttonSmall">
	<input type="button" name="btnSquare" value="sqrt" class="buttonSmall"><br/>
	
	<input type="button" name="btnMr" value="MR" class="buttonSmall">
	<input type="button" name="btnFour" value="4" class="buttonSmall">
	<input type="button" name="btnFive" value="5" class="buttonSmall">
	<input type="button" name="btnSix" value="6" class="buttonSmall">
	<input type="button" name="btnMult" value="*" class="buttonSmall">
	<input type="button" name="btnPercent" value="%" class="buttonSmall"><br/>
	
	<input type="button" name="btnMs" value="MS" class="buttonSmall">
	<input type="button" name="btnOne" value="1" class="buttonSmall">
	<input type="button" name="btnTwo" value="2" class="buttonSmall">
	<input type="button" name="btnThree" value="3" class="buttonSmall">
	<input type="button" name="btnSub" value="-" class="buttonSmall">
	<input type="button" name="btnFrequency" value="1/x" class="buttonSmall"><br/>
	
	<input type="button" name="btnMplus" value="M+" class="buttonSmall">
	<input type="button" name="btnZero" value="0" class="buttonSmall">
	<input type="button" name="btnSignChange" value="+/-" class="buttonSmall">
	<input type="button" name="btnDecimal" value="." class="buttonSmall">
	<input type="button" name="btnAdd" value="+" class="buttonSmall">
	<input type="button" name="btnEqual" value="=" class="buttonSmall"><br/>
</div>	
</form>


</body>

</html>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Please go back and read Everah's first response again. PHP is server scripting, Javascript is browser scripting. If someone gave you an assignment to handle user input using PHP, it is a ridiculous assignment. There's no point in learning something that is totally useless in real life.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You onClick is going to fail everytime for the reason that Don and I have mentioned. You cannot expect something on the client to do something on the server without Ajax, and in this case you are not using it.

Also, you need to do a check for $_POST['inputText'] or you will throw an undefined index error on first load.
rafael.lucchese
Forum Newbie
Posts: 8
Joined: Fri Oct 19, 2007 7:56 am

Post by rafael.lucchese »

Thank you both for your replies.

Yes sir, I also think that you have a valid point and I respect it. Unfortunately, I really have to complete the assignment with the constraints that were presented to me, and JavaScript was not mentioned (only HTML, CSS, and PHP). Either way, I thank you for all of the help that I have had so far here (all parties).

The input text field is responding to the button commands now, but it only responds to the first two submit buttons that I declared (at first). When I first load the calculator, I have to click on either button 7 or 8 to get a response (which are the first two that I declared). If I click on one of those two, the other buttons work; if not, I get no response from buttons other 7 or 8 at first attempt.

Is there a limitation on the number of submit buttons that I can use in a form? If not, would any of you know at a glance what I am making a mistake at? I named all of the buttons the same name...I am not really sure why, but I wanted a generic form since the buttons have diferent values that are submitted.

Thank you in advance,

Rafael.

Code: Select all

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

<?php
	
	$inputText = $_POST['button'];

	//in the display ("0." has to be replaced with the number plus ".")
	//$isFirstOperand = true; // Determines if the operand being stored is the one
	//to the left side of the operator
	//$operator = ""; // Will store the name of the operator upon onRelease of a #btn
	//$reg1 = 0; // Stores a value in the memory. It will be a number
	//$operand1 = 0; //Stores first value in an operation statement
	//$operand2 = 0; //Stores the second value in the operation
	
	//$formatted = number_format($number, 2);

//During the second operation, the program needs to be aware that a first
//operand and an operator already exist
function secondOperand($isFirstOperand) 
{
	$isFirstOperand = false; // Defines that the number is the second operand	
	return $isFirstOperand;
}

function doOperation($operand1, $operand2, $operator)
{
	if(is_numeric($inputText)) /* */
	{
		if($operator != null) /* Operator info stored here */
		{
			switch($operator)
			{
				case "btnAdd" : $result= $operand1 + $operand2; break;
				case "btnSub" : $result= $operand1 - $operand2; break;
				case "btnMult" : $result= $operand1 * $operand2; break;
				case "btnDiv" : $result= $operand1 / $operand2; break;
			}
	
			echo("Calculation result: $result");
			//For testing only, this needs to output to the text box
		}
	}
	else
	{ 
		echo("Invalid entry - please retry with numbers only.");
	}
}

?>


<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/>
	<div id="topLeftSquare"></div> <!-- Irrelevant, but present in the windows calc. -->
	
	<input type="button" name="btnBackspace" value="Backspace" class="buttonLarge">
	<input type="button" name="btnCe" value="CE" class="buttonLarge">
	<input type="button" name="btnC" value="C" class="buttonLarge"><br/>
	
	<input type="button" 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="button" name="btnDiv" value="/" class="buttonSmall">
	<input type="button" name="btnSquare" value="sqrt" class="buttonSmall"><br/>
	
	<input type="button" 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="button" name="btnMult" value="*" class="buttonSmall">
	<input type="button" name="btnPercent" value="%" class="buttonSmall"><br/>
	
	<input type="button" 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="button" name="btnSub" value="-" class="buttonSmall">
	<input type="button" name="btnFrequency" value="1/x" class="buttonSmall"><br/>
	
	<input type="button" name="btnMplus" value="M+" class="buttonSmall">
	<input type="submit" name="button" value="0" class="buttonSmall">
	<input type="button" name="btnSignChange" value="+/-" class="buttonSmall">
	<input type="button" name="btnDecimal" value="." class="buttonSmall">
	<input type="button" name="btnAdd" value="+" class="buttonSmall">
	<input type="button" name="btnEqual" value="=" class="buttonSmall"><br/>
</div>	
</form>


</body>

</html>
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

Okay - besides the point that this is pointless, none the less it is your task at hand so let's put our heads together and figure it out.l

I once created a similar idea, the person would choose website pieces (each with a different dollar amount) and when they hit submit the php would tally the totals and list them, you could do the following:

use sessions to hold the form data while the user is at your site (so no matter what page they are at, as long as you 'session_start()' at the top of the php, you can grab that form data - it's kind of like a cookie - but with no time limit). If they punch the number 5 that will be sent to the server, the page will refresh to another one or the same (that's your choice), then their is a textbox that has the inputted value from the textbox before in the value area as $value. I am not amazing at php so I can't give you exact code, but I know that it can be accomplished. Good luck.

Code: Select all

<input type="textbox" name="value" value="$value">
Hope that helps.

Sorry if I have totally gone off on another tangent (I love rabits).

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

Post by rafael.lucchese »

Thanks Greg,

I think that your advice is really good. Talking about sessions, I am thinking now that I need to append to the text field variable contents, and send everything to the server at once for computation. What I mean is that, once I have the second operand and I press the equal sign or another operator, I have to submit a text field with the operator and the two operands to the server, wehre it will be divided using string functions, assigned to different variables like the $operand1 and $operand2 that I have, and the $operator, and then I will run it trough my doOperation() function. Once that is done, I can update the text field or simply create another one like you said, I am thinking.

I guess I don't know how I can hold the value of the text field, while I clear it for mor input and I also have no Idea how to append to the variable (especially without contacting the server).

At this point I don't know if I should use at least some JavaScript to hold variable values, and then submit them to the server all at once. Is there any way to do that in PHP, to hold values of buttons pressed and keep appending to that value before I contact the server for the complete operation (left operand, operator, right operand = result back to the client)?

Also, I have not figured out how I will represent the "." that stays on the right side of the calculator (although my thoughts were to use string concatenation, I am dealing with numeric values in a loosely typed language).

Let me know if you can give me any more advice on this.

Thanks again Greg,

Rafael .
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You don't need Javascript and using a form is the hard way to do it. Just make each button a link and use PHP to add the current "inputText" to the URL as a second parameter.

Examples:

Code: Select all

<a href="myscript.php?button=MC&inputText=<php echo $inputText; ?>" class="buttonSmall">MC</a>
        <a href="myscript.php?button=7&inputText=<php echo $inputText; ?>" class="buttonSmall">7</a>
It's very RESTful and leaves you to focus on filtering, validating and escaping your input -- which is where your programming skills should be focused.
(#10850)
rafael.lucchese
Forum Newbie
Posts: 8
Joined: Fri Oct 19, 2007 7:56 am

Post by rafael.lucchese »

Thank you :)

I think the idea sounds great, but I do not really understand the sintax and how it can be applied. I am assuming that the value of the buttons will be stored in $_GET['button'] and $_GET['value'], then I can use string functions to just append to the values. If I go that route, would I have to use fireworks to draw buttons and CSS to display them, or can I take advantage of the buttons that I already have and just have them sitting inside of the "a href" tag with some kind of a null value?

Sorry, I have been looking up great deals of information in PHP in very little time; I am still a little confused.

Thanks for your help,

Rafael.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It would be $_GET['button'] and $_GET['inputText'] from the code you posted. Yes, you can just wrap you button images in <a> tags (remember to set the border to 0).
(#10850)
Post Reply