0

I am working on a project. For my project, it requires user to enter inflation rate and rate of return of investment. If the user didn't enter anything, it will have default values of 3 and 4 respectively. Then if the user entered whatever value, it will be the value. (if the user entered 0, it will still post the 0. I have tried the following code but the default values do not work. Thank you for your time.

<html> <p> <b>Expected rate of return for investment (%):&nbsp;&nbsp;&nbsp;&nbsp;</b> <input type="number" value="<?php echo $_POST['rinvestment'];?>" min = "0" placeholder= "4" name="rinvestment" /> </p> <p> <b>Inflation Rate (%): </b> <input type="number" value="<?php echo $_POST['inflation'];?>" min = "0" step = "0.1" name="inflation" placeholder= "3"/> </p> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST['rinvestment'])){ $rinvestment=$_POST['rinvestment']; } else { $rinvestment = 4; } if (isset($_POST['inflation'])){ $inflation=$_POST['inflation']; } else { $inflation = 3; } } ?> 

5
  • Well you might want to read up on with ISSET() actually does... Commented Aug 21, 2015 at 7:32
  • I am confused as to what you are trying to do. Can you make it more clear? Why are you checking for isset of a POST variable without sending any POST request? Commented Aug 21, 2015 at 7:33
  • @AcharyaAnurag sorry i didn't copy all my code, there are submit button too, so if the user click the submit button, it will post the value. Commented Aug 21, 2015 at 7:37
  • How do you want it to be if user enters 0 ? Commented Aug 21, 2015 at 7:38
  • @SerhatAkay then the value will be 0. thank you Commented Aug 21, 2015 at 8:06

2 Answers 2

2

The purpose of the isset() variable is just as the name suggests - to check if a particular variable is set, i.e. if it exists.

In your case, despite the user not setting any values, the $rinvestment and $inflation variable are being sent, albeit as a null variable. Now since the isset() function always find the variable, it always returns true. This is why your code is not working.

You need to check for an empty variable to satisfy your condition, i.e.

if(!empty($inflation)) { //set default value } 

Although this gets your job done, it is recommended to check for isset of the POST variables to avoid errors.

Sign up to request clarification or add additional context in comments.

7 Comments

I did try that but when user enters 0, the webpage will assume user didn't enter anything, so it will calculate use the default numbers which is not i want. Thank you for your time
In that case, you could make use of a simple radio button to ask user whether or not they want to add a custom value. This way your task becomes pretty simple!
Can I achieve that by just add few lines of coding?
Yes. If you could provide me with your full code, I could tweak it easily. It would save me the trouble of having to write the whole code myself.
However, I do not want to have any more buttons. For the type I want is it impossible? Like when the user enters 0, then it will post 0 but there is default values when !empty.
|
0

isset() only checks if the value is set, the value can still be empty so try something like

if(isset($_POST["rinvestment"]) && !empty($_POST["rinvestment"])) { $rinvestment=$_POST['rinvestment']; } else { $rinvestment = 4; } 

if you want the if the user has 0 in the input field that it uses the default value, use this:

if(isset($_POST["rinvestment"]) && (int) $_POST["rinvestment"] > 0) { $rinvestment=$_POST['rinvestment']; } else { $rinvestment = 4; } 

1 Comment

Thank you. This works, however, when user enter 0, the webpage will still think that is empty then return the default value. I hope when the user enter 0, it will post 0.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.