Jump to content
xisto Community
darran

Submit Restrictions Is there anyway to bypass this on my localhost?

Recommended Posts

As for the validation, I am only checking for the numeric numbers using is_numeric() but also I need to validate whether a radio button is selected and whether the user has entered anything. All that is fine with the exception of the reset button not doing what it should. Resetting the result to 0 or emptyI did as you said, setting the initital value of input fields to '0' but when I clicked the reset button, my $result variable value did not reset along with the form entered by the user.

Share this post


Link to post
Share on other sites

I can't see where you are re-setting the txt1stnumber or the txt2ndnumber variables, so they are probably producing the result and the result is being echo'd out each time.Maybe re-post the current code here.

Share this post


Link to post
Share on other sites

Here is the code

<?php		function calculate() {			$result = 0;			$_1stNumber = 0;			$_2ndNumber = 0;			if (isset($_POST['submitted'])){				$_1stNumber = stripslashes(trim($_POST['txt1stNumber']));				$_2ndNumber = stripslashes(trim($_POST['txt2ndNumber']));				if (!is_numeric($_1stNumber) || !is_numeric($_2ndNumber)) {					exit("<font color=#FF0000>Enter a number in the textbox</font>");				}				if (isset($_POST['rad1'])){					$rad1 = $_POST['rad1'];						switch($rad1){							case "Add": $result = $_1stNumber + $_2ndNumber;							break;							case "Subtract": $result = $_1stNumber - $_2ndNumber;							break;							case "Multiply": $result = $_1stNumber * $_2ndNumber;							break;							case "Divide": $result = $_1stNumber / $_2ndNumber;							break;						}					echo("<font color=#FF0000>$result</font>");				} else {					exit("<font color=#FF0000>Select an operation</font>");				}			}		}	?>

However I feel this is not the right way, this is just setting the starting value of the 2 variables $_1stNumber and $_2ndNumber to 0. And clicking on the reset button does not help in changing this value back to 0. Is there a way to check when the reset button is pressed so that I can reset the value of the result there? On a general question, is there anyway to handle button clicks other than using javascript because not everyone would have javascript enabled.

Share this post


Link to post
Share on other sites

I'm not quite sure what you're asking, but if you mean how do you reset the variables within PHP when the Reset button is pressed... the value is only going to be sent to the server when the user clicks 'Submit'. The Reset button is handled only on the client-side, so clicking it doesn't interact with the server at all. The variables are going to contain the value they were initially assigned (in this case, the integer value '0') each time the script is executed, and the values will not be 'remembered'.

 

And no, the only way to handle button clicks (and most other client-side events outside of the standard markup) is via JavaScript. Most people have it enabled.

Share this post


Link to post
Share on other sites

My question was a way to reset the values by pressing a button, it is a default reset button but I want it to be able to reset my values to the original ones I defined.

In other words, I can reset the value of the variable using javascript? Something along the lines of

<script language="javascript" for="btnReset" event="onclick"><? php $result = 0; ?></script>

Please correct me on this? But lets say a person decides to disable javascript for whatever wierd reason, is there a way to handle button clicks in a web-based environment?

Share this post


Link to post
Share on other sites

It doesn't work like that. PHP is only server-side, and JavaScript is only client-side. As I said, the variables are going to have whatever value you assign them in the script. You can't assign them a value of '0', and then expect them to retain that value on later executions, because they won't. The only way you can do that is by storing the value somewhere, and recalling it later. If someone executes your script to add the values '2' and '3' together, the script isn't going to 'remember' that it used '2' on the next run, so the variable isn't going to have a default value of '2' or anything. I think that's what you're getting at, anyway.

Share this post


Link to post
Share on other sites

I know what you are getting at. For my problem, I want the result to be reflected back at the original value. So after clicking the default reset button, I want the result to be shown as 0 or something like that. As you said Javascript handles button clicks on the client side, so I want javascript to reset the value but this variable is a php variable, how can I port it into javascript so that I can reset it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.