Jump to content

[SOLVED] code falls through If statement whether true or false


p3aul

Recommended Posts

Hi!

I have a preg_match verifying an input from a form. It works fine except for the first time page is loaded. On page load the code falls through and displays my error msg. I input the correct format and the error goes away and my program functions normally. If I then input a letter9which I don't want0 everything is normal and the error message is displayed. The code is below, including the html form.

 

<div id="Layer1"><img src="Untitled-1.gif" width="375" height="500" /></div>
<div id="Layer2">
  <form id="form1" name="form1" method="post" action="Untitled-2.php">
    <label> <br /> 
    <input name="db" type="text" size="4" maxlength="6" />
    <span class="style1">Dry Bulb Temp(Celsius)</span></label>
    <p>
      <label> 
      <input name="wb" type="text" size="4" maxlength="6" />
      <span class="style3">Wet Bulb Temp(Celsius)</span></label>
    </p>
    <p>
      <label> 
      <input  name="pr" type="text" size="4" maxlength="6" />
      <span class="style4">Ambient Pressure(Millibars)</span></label>
    <br>
    <br>         
   
<label> 

      <input type="submit"  name="Submit" value="Calculate" />
      </label>
    </p>
</form></div>
<div id="Layer3"><?php
  
  
$db = $_POST["db"];
$wb = $_POST["wb"];
$pr = $_POST["pr"];
//
// For testing purposes only I'm only verifying $db
//
//if(strcmp($_POST["submit"],"submit")){echo "<input type='text' size='14' name='error' value='No Field Left Blank!'>";}
if ( ! preg_match('/^\d+$/', $db)){
echo "<br>";
echo "&nbsp";
echo "&nbsp";
echo "<input type='text' size='14' name='error' value='Numbers Only!'>";}
// Calculate Ew, the wetbulb vapor pressure component
else {
$F = 6.112 * exp((17.67*$wb)/($wb+243.5));
//
//Reassignes $F to the more readable Es 
$Ew = $F;
//

// Calculates Es, the dry bulb vapor pressure component

$H = 6.112 * exp((17.67*$db)/($db+243.5));
//
// and reassigns it to Es
$Es = $H;
//
// Actual Vapor pressure 
$I = $Ew - $pr*($db-$wb)*.00066*(1+(.00115*wb));
//
//Reassigns it 
$Vpres = $I;
//
// Calculate Relative Humidity
$J = ($Vpres/$Es)* 100;
$rh = $J;
$rh = round($rh,3);
// Calculate Dewpoint


$do = $Vpres/6.112;
$logs = Log10($do); 
$Dp = round(((237.7 * $logs)/(7.7-$logs)),3);
//
echo "<br>";
echo "&nbsp";
echo "&nbsp";
//if($db == ""){$rh = 0;}
echo "<input type='text' size='4' name='name' value='$rh'> % Relative Humidity";
echo "<br>";
echo "<br>";
echo "&nbsp";

echo " <input type='text' size='4' name='name' value='$Dp'> Dewpoint";}

?>

 

I hope some one can help me, I'm pulling all my hair out and I'm already bald!

Paul

Link to comment
Share on other sites

You should only execute the PHP code after the form has been submitted. Since PHP runs before the form is displayed on the browser, it doesn't see any posts the first time.

 

Do something like this:

<div id="Layer3"><?php
if isset($_POST['submit'])) {
$db = $_POST["db"];
$wb = $_POST["wb"];
$pr = $_POST["pr"];
//
// For testing purposes only I'm only verifying $db
//
//if(strcmp($_POST["submit"],"submit")){echo "<input type='text' size='14' name='error' value='No Field Left Blank!'>";}
if ( ! preg_match('/^\d+$/', $db)){
	echo "<br>";
	echo "&nbsp";
	echo "&nbsp";
	echo "<input type='text' size='14' name='error' value='Numbers Only!'>";}
// Calculate Ew, the wetbulb vapor pressure component
else {
	$F = 6.112 * exp((17.67*$wb)/($wb+243.5));
	//
	//Reassignes $F to the more readable Es 
	$Ew = $F;
	//

	// Calculates Es, the dry bulb vapor pressure component

	$H = 6.112 * exp((17.67*$db)/($db+243.5));
	//
	// and reassigns it to Es
	$Es = $H;
	//
	// Actual Vapor pressure 
	$I = $Ew - $pr*($db-$wb)*.00066*(1+(.00115*wb));
	//
	//Reassigns it 
	$Vpres = $I;
	//
	// Calculate Relative Humidity
	$J = ($Vpres/$Es)* 100;
	$rh = $J;
	$rh = round($rh,3);
	// Calculate Dewpoint


	$do = $Vpres/6.112;
	$logs = Log10($do); 
	$Dp = round(((237.7 * $logs)/(7.7-$logs)),3);
	//
	echo "<br>";
	echo "&nbsp";
	echo "&nbsp";
	//if($db == ""){$rh = 0;}
echo "<input type='text' size='4' name='name' value='$rh'> % Relative Humidity";
echo "<br>";
echo "<br>";
echo "&nbsp";

echo " <input type='text' size='4' name='name' value='$Dp'> Dewpoint";}
}
?>

 

Ken

 

 

Link to comment
Share on other sites

if isset($_POST['submit'])) {

 

That looks like it could be the answer, however, I can't get past syntax errors. Used as is, I get a:

 

"Parse error: syntax error, unexpected T_ISSET, expecting '(' in C:\xampp\htdocs\Untitled-2.php on line 66"

 

I can't understand why there is one "(" after if and two ")" after the "]" also there is an opening brace but no closing brace. should I put a closing brace after $pr = $_POST["pr"];

I'm sorry I'm such a dunce at this but I am relatively new to PHP although I certtainly prefer it to Java applets!

Thanks,

Paul

Link to comment
Share on other sites

Ok, I had left out the "(" after the if. I also didn't see the closing "}" at the end of the code.

 

Now The page loads with out falling through. BUT This code is never executed.

 

echo "<input type='text' size='4' name='name' value='$rh'> % Relative Humidity";
echo "<br>";
echo "<br>";
echo "&nbsp";

echo " <input type='text' size='4' name='name' value='$Dp'> Dewpoint";

 

And when I input an intensional "a" , I dot get that error out either.

 

echo "<input type='text' size='14' name='error' value='Numbers Only!'>";}

 

Thanks, guys for keeping up the help!

Paul

 

Link to comment
Share on other sites

Ok, The program I am trying to troubleshoot is too big for me to see the trees so I threw this together just to test it out.  If I leave te code as is It doesn't fall through BUT neither does it execute the output. If I comment out the isset phrase, it fall through, but it executes the output. hmmm. I think it might have to do with e braces or "()" being in the wrong place. But what is the solution. with this simple program the answer should be obvious, but I can't see it. Any thoughts Please?

Paul :(

<body>
<form id="form1" name="form1" method="post" action="Untitled-4.php">
    <label> <br /> 
    <input name="db" type="text" size="4" maxlength="6" />
    <span class="style1">Dry Bulb Temp(Celsius)</span></label>
    <p>
      

      <input type="submit"  name="Submit" value="Calculate" />
      </label>
    </p>
</form>
<?php
  
if (isset($_POST['submit'])) {  
$db = $_POST["db"];
if ( ! preg_match('/^\d+$/', $db)){
echo "<br>";

echo "<input type='text' size='14' name='error' value='Numbers Only!'>";}
  else{ echo "<br>";
echo "<input type='text' size='4' name='error' value='$db'>";}}
?>  
</body>

Link to comment
Share on other sites

Why are you outputting the error messages as "<input>" tags? Shouldn't they be plain messages?

 

Well the whole picture includes an image I created in photoshop to serve as a backgroud for the input an below that, the output. It took some finagling to get the txtfields to line up correctly with the image.Thats why all the <br> and &nbsp . The form is below. If it would help i'll post the entire code. As I'm not the best coder in the world, I wrote the math in bite sized chunks that i could test as I went along. For a quick and dirty page I wrote something that I could test the logic and this is what I came up with:

 

<body>
<form id="form1" name="form1" method="post" action="Untitled-4.php">
    <label> <br /> 
    <input name="db" type="text" size="4" maxlength="6" />
    <span class="style1">Dry Bulb Temp(Celsius)</span></label>
    <p>
      

      <input type="submit"  name="Submit" value="Calculate" />
      </label>
    </p>
</form>
<?php
  
if (isset($_POST['submit'])) {
$db = $_POST["db"];


echo $db;}
?>  
</body>

 

As you can see, this is the original code(with your isset addition) reduced to the basics.

echo $db; is never executed. remove the isset by commenting it out and the code is executed.

I can't understand why.

Paul

 

I tried to up load the gif file i used as the background and the original php file but the forum software won't let me I will cut and paste the php file.

 

Sorry this post is so long :(

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test PHP</title>
<style type="text/css">
<!--
body {
background-color: #AAFF00;
}
#Layer1 {
position:absolute;
left:326px;
top:40px;
width:329px;
height:479px;
z-index:1;
}
#Layer2 {
position:absolute;
left:375px;
top:104px;
width:282px;
height:136px;
z-index:2;
}
#Layer3 {
position:absolute;
left:405px;
top:413px;
width:257px;
height:78px;
z-index:3;
}
-->
</style></head>

<body>

<div id="Layer1"><img src="Untitled-1.gif" width="375" height="500" /></div>
<div id="Layer2">
  <form id="form1" name="form1" method="post" action="Untitled-2.php">
    <label> <br /> 
    <input name="db" type="text" size="4" maxlength="6" />
    <span class="style1">Dry Bulb Temp(Celsius)</span></label>
    <p>
      <label> 
      <input name="wb" type="text" size="4" maxlength="6" />
      <span class="style3">Wet Bulb Temp(Celsius)</span></label>
    </p>
    <p>
      <label> 
      <input  name="pr" type="text" size="4" maxlength="6" />
      <span class="style4">Ambient Pressure(Millibars)</span></label>
    <br>
    <br>         
   
<label> 

      <input type="submit"  name="Submit" value="Calculate" />
      </label>
    </p>
</form></div>
<div id="Layer3"><?php
  
if (isset($_POST['submit'])) {  
$db = $_POST["db"];
$wb = $_POST["wb"];
$pr = $_POST["pr"];
//
// For testing purposes only I'm only verifying $db
//
//if(strcmp($_POST["submit"],"submit")){echo "<input type='text' size='14' name='error' value='No Field Left Blank!'>";}
if ( ! preg_match('/^\d+$/', $db)){
echo "<br>";
echo "&nbsp";
echo "&nbsp";
echo "<input type='text' size='14' name='error' value='Numbers Only!'>";}
// Calculate Ew, the wetbulb vapor pressure component
else {
$F = 6.112 * exp((17.67*$wb)/($wb+243.5));
//
//Reassignes $F to the more readable Es 
$Ew = $F;
//

// Calculates Es, the dry bulb vapor pressure component

$H = 6.112 * exp((17.67*$db)/($db+243.5));
//
// and reassigns it to Es
$Es = $H;
//
// Actual Vapor pressure 
$I = $Ew - $pr*($db-$wb)*.00066*(1+(.00115*wb));
//
//Reassigns it 
$Vpres = $I;
//
// Calculate Relative Humidity
$J = ($Vpres/$Es)* 100;
$rh = $J;
$rh = round($rh,3);
// Calculate Dewpoint


$do = $Vpres/6.112;
$logs = Log10($do); 
$Dp = round(((237.7 * $logs)/(7.7-$logs)),3);
//
echo "<br>";
echo "&nbsp";
echo "&nbsp";
//if($db == ""){$rh = 0;}
echo "<input type='text' size='4' name='name' value='$rh'> % Relative Humidity";
echo "<br>";
echo "<br>";
echo "&nbsp";

echo " <input type='text' size='4' name='name' value='$Dp'> Dewpoint";}}

?>
</div>
</div>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.