Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maintaning the Value of variable.

Status
Not open for further replies.

tekpr00

IS-IT--Management
Jan 22, 2008
186
CA
I have one problem, while I am asking user for correction like so "Please enter numeric value for Income". however the value of Expenses disapper, and user have to enter it again. how can I make it so that it still save or maintains the already entered value for expenses while asking for correction of other fiels. Also, how come my print out is not displaying. Thanks in advance for your help

Income:

Expenses:

Debt:

Please enter numeric value for Income

+++++++++++++++++++++
Here is my code
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Finance </title>
  <style type="text/css" media="screen">.error {color: red ;} </style>
 </head>
 <body>

 <form action="finance.php" method="post">

 <p>Income: <input type="text"
 name="income" size="10" /></p>

<p> Expenses: <input type="text"
 name="expenses" size="10" /></p>

<p> Debt: <input type="text"
 name="debt" size="10" /></p>

 <input type="submit" name="submit"
 value="Calculate" />

 </form>

<?php 
//flag variable to track success:
$okay = TRUE;


//check to see if there is value in income
if (empty($_POST['income'])) {
	print '<p class="error"<The income field is empty, please complete</font>. </p>';
} elseif (!is_numeric($_POST['income'])){
    print '<p class="error">Please enter numeric value for income </p>';
} elseif ($_POST['income'] < 0 ) {
	print '<p class="error">Please enter non-negative income value. </p>';
} elseif (empty($_POST['expenses'])) {
	print '<p class="error"<The Expenses field is empty, please complete</font>. </p>';
} elseif (!is_numeric($_POST['expenses'])){
    print '<p class="error">Please enter numeric value for Expenses </p>';
} elseif ($_POST['expenses'] < 0 ) {
	print '<p class="error">Please enter non-negative Expenses value. </p>';
} elseif (empty($_POST['debt'])) {
	print '<p class="error"<The Debt field is empty, please complete</font>. </p>';
} elseif (!is_numeric($_POST['debt'])){
    print '<p class="error">Please enter numeric value for Debt </p>';
} elseif ($_POST['expenses'] < 0 ) {
	print '<p class="error">Please enter non-negative Debt value. </p>';
}
	else {//Problem
	print '<p class="error">Please complete all fields </p>';
	$okay =FALSE;
}

if ($okay) { 

	print "<div><p>Your Income is $<span class=\"number\">$income</span><br />
	       Your Expenses is $<span class=\"number\">$expenses</span><br />
		  Your Debt is $<span class=\"number\">$debt</span><br /></p></div>";

}
?>
</body>
</html>
 
I have one problem, while I am asking user for correction like so "Please enter numeric value for Income". however the value of Expenses disapper, and user have to enter it again. how can I make it so that it still save or maintains the already entered value for expenses while asking for correction of other fiels.

You need to store the submitted values and output them into the appropriate fields.

I would have all the PHP code before the form html, and then just add the values as necessary.

Code:
 <form action="finance.php" method="post">

 <p>Income: <input type="text"
 name="income" size="10" [red]value="[green]<?PHP if(isset($_POST['income']){echo $_POST['income'];}?>[/green]"[/red] /></p>

<p> Expenses: <input type="text"
 name="expenses" size="10" [red]value="[green]<?PHP if(isset($_POST['expenses']){echo $_POST['expenses'];}?>[/green]"[/red] /></p>

<p> Debt: <input type="text"
 name="debt" size="10" [red]value="[green]<?PHP if(isset($_POST['debt']){echo $_POST['debt'];}?>[/green]"[/red] /></p>

 <input type="submit" name="submit"
 value="Calculate" />

 </form>

Also, how come my print out is not displaying. Thanks in advance for your help

Your last else{} statement will fire when everything is correct, thus setting your okay variable to false. So your final print display never happens because it needs $okay to be true to run the code inside the if statement.

Basically your last else is reversed. You should be setting your variable to true, and maybe output a message saying everything is o.k.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks for your second sugestion, however, with the first suggestion, I will like to keep the format as is. So if know a way that I can use to keep variable the same I will appreciate it.
 
You can ignore the "I would have all the PHP code before the form html, and then just add the values as necessary. " comment. You can keep your code as is, and just add the lines as shown above in green and red.

Other than that there's no other way to get the values to be put back into the input elements.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top