p3aul Posted October 9, 2008 Share Posted October 9, 2008 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 " "; echo " "; 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 " "; echo " "; //if($db == ""){$rh = 0;} echo "<input type='text' size='4' name='name' value='$rh'> % Relative Humidity"; echo "<br>"; echo "<br>"; echo " "; 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 9, 2008 Share Posted October 9, 2008 I'd bet that it's because $db isn't set after the refresh because it was just from a POST variable. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 9, 2008 Share Posted October 9, 2008 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 " "; echo " "; 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 " "; echo " "; //if($db == ""){$rh = 0;} echo "<input type='text' size='4' name='name' value='$rh'> % Relative Humidity"; echo "<br>"; echo "<br>"; echo " "; echo " <input type='text' size='4' name='name' value='$Dp'> Dewpoint";} } ?> Ken Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 9, 2008 Share Posted October 9, 2008 I'd bet that it's because $db isn't set after the refresh because it was just from a POST variable. EDIT: That should have read: I bet that's because $db isn't set before you submit the form because it is just from a POST variable. Woops. >_< Quote Link to comment Share on other sites More sharing options...
p3aul Posted October 9, 2008 Author Share Posted October 9, 2008 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 9, 2008 Share Posted October 9, 2008 That should be <?php if (isset($_POST['submit'])) { ?> I said it was untested... Ken Quote Link to comment Share on other sites More sharing options...
p3aul Posted October 9, 2008 Author Share Posted October 9, 2008 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 " "; 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 Quote Link to comment Share on other sites More sharing options...
p3aul Posted October 9, 2008 Author Share Posted October 9, 2008 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> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 9, 2008 Share Posted October 9, 2008 Why are you outputting the error messages as "<input>" tags? Shouldn't they be plain messages? Ken Quote Link to comment Share on other sites More sharing options...
p3aul Posted October 9, 2008 Author Share Posted October 9, 2008 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   . 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 " "; echo " "; 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 " "; echo " "; //if($db == ""){$rh = 0;} echo "<input type='text' size='4' name='name' value='$rh'> % Relative Humidity"; echo "<br>"; echo "<br>"; echo " "; echo " <input type='text' size='4' name='name' value='$Dp'> Dewpoint";}} ?> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
AndyB Posted October 9, 2008 Share Posted October 9, 2008 Your real problem is that you are testing for the non-existent variable named submit. The real name of your input is Submit. Quote Link to comment Share on other sites More sharing options...
p3aul Posted October 9, 2008 Author Share Posted October 9, 2008 Well i be Durned! It's amazing that the lack of a capital letter caused that! My hats off to you, Andy! Hey and I still have a few hairs left! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.