Page 1 of 1

error combining html & php on same page

Posted: Sat Sep 06, 2008 12:52 pm
by p3aul
Hi!
I am developing a humidity calculator in php for my own use so I'm not concerned with validation or security. Here is the HTML:

Code: Select all

<body>
<form id="form1" name="form1" method="post" action="rh.php">
  <label>
  <input type="text" name="db" />
    Dry Bulb Temp(Celsius)</label>
  <p>
    <label>
    <input type="text" name="wb" />
    Wet Bulb Temp(Celsius)</label>
  </p>
  <p>
    <label>
    <input type="text" name="pr" />
    Ambient Pressure(Millibars)</label>
</p>
  <p>
    <label>
     <input type="submit" name="Submit" value="Calculate" />
    </label>
  </p>
</form>
Here is the PHP:

Code: Select all

<?php
$db = $_POST["db"];
$wb = $_POST["wb"];
$pr = $_POST["pr"]
$F = pow((6.112*2.178),((17.67*$wb)/($wb+243.5)));
$G = round($F,3);
echo $G;
?>
The php code works if I use straight numbers and no variables. It's when I put in the form that I have problems. I named my file rh.php and ran it on my local server. I am using Dreamweaver 8 to code the page.
Thanks,
Paul

Re: error combining html & php on same page

Posted: Sat Sep 06, 2008 1:03 pm
by Cut
Well, you forgot a semicolon on line 4 of your php. Also, most people use single quotes within square brackets ($_POST['db']). I don't know if that's necessary, but it should be faster.

btw, "problems" is WAY too vague.

Re: error combining html & php on same page

Posted: Sat Sep 06, 2008 1:39 pm
by p3aul
I should have been clearer, but I figured someone could "see" my code better than I could and spot a syntax error, as you did!

Thanks for responding so quickly, Cut! It was the lack of a semicolon that did it. :D I have tried to code this in several different languages, but I get so bogged down in text to double number conversions and their limitations that I decided to go with PHP instead. At least it's a more flexible language.
Thanks,
Paul

Re: error combining html & php on same page

Posted: Sat Sep 06, 2008 2:51 pm
by markusn00b
For the missing semi-colon you would've received an error. It is helpful if you provide the error that you receive.

Re: error combining html & php on same page

Posted: Sat Sep 06, 2008 5:47 pm
by p3aul
Sorry! Next time I will. Funny, I had copied the error msg to the clipboard intending to paste it in and then forgot it!

All is not well though! I first created the formula in Mathcad and tested it against and online calculator and it gave me the correct answer. This formula that I pasted here does not give me the correct answer. In the formula above I wrote it all out as a complicated formula. I again went back to the drawing board and wrote it piecemeal. I first calculated the math for the exponent then used that answer to get another result etc. I got the same answer as the first time. The wrong answer, so I know the coding of the formula is correct, at least about 80% sure! The online calculator I used to check my work is located here: http://www.ringbell.co.uk/info/humid.htm If anyone wants to double-check my work and tell me where I went wrong please do. :banghead:
Paul

Re: error combining html & php on same page

Posted: Sat Sep 06, 2008 6:37 pm
by paperplate
p3aul wrote:The online calculator I used to check my work is located here: http://www.ringbell.co.uk/info/humid.htm If anyone wants to double-check my work and tell me where I went wrong please do. :banghead:
Paul
Looking at the JS for that page, I saw this:

Code: Select all

Ew = 6.112 * (Math.pow(Math.E, ((17.67 * WetBulbc) / (243.5 + WetBulbc))));
I'm assuming that's what you're basing your code off of.

Comparing the above with what you posted:

Code: Select all

$F = pow((6.112*2.178),((17.67*$wb)/($wb+243.5)));
I see one error is that 6.112 should not be part of the pow() function. It also seems that it is simple raising "e" to the power of ((17.67 * WetBulbc) / (243.5 + WetBulbc)). You can use the exp() function vs. pow() for this.

Therefore, I think the correct code would be:

Code: Select all

$F = 6.112 * exp((17.67*$wb)/($wb+243.5));

Re: error combining html & php on same page

Posted: Sat Sep 06, 2008 11:55 pm
by p3aul
I'm assuming that's what you're basing your code off of.
Actually, no. I used his calculator but not his code. I got the code from this site:
http://www.srh.noaa.gov/epz/wxcalc/form ... tBulb.html
Even this one has an error in calculating the Dew-point so I went to another site to get the correct one. Both sites were NOOA by the way.

As I look at this site now I don't know why I didn't just use "e, they did. Using your code I get a slightly different answer than in my Mathcad project. Using the corrected code(now!) I get 66.77% My Mathcad answer was 66.546% using 30 C db and 25 C wb and 1008 millibars. Using the same figures, his answer was 66.5% So my Mathcad project gave me the same answer as the online calculator. Steve Scanlon in his JS code used e just like you did in the PHP code you revised and I used the rounded off version in Mathcad. Does this mean JS is more precise than PHP? This has me puzzled and I won't rest until I get it figured out! Thanks,,for rewriting the code and pointing out my error, by the way! My next step is changing the 2.718 in Mathcad to "e" :idea:
Paul
My next step is changing the 2.718 in Mathcad to "e"
Nope! Didn't change a thing. I guess the answer lies in the way JS math functions are written as opposed to PHP. The difference does't really mean a thing because I believe the formulas only give an approximate answer anyway!

Re: error combining html & php on same page

Posted: Sun Sep 07, 2008 10:00 am
by paperplate
I get 66.5% as well with the same code, unless you're doing something different after your $F calculation. This is what I tried (I used $_REQUEST for simplicity):

Code: Select all

 
<pre>
<?php
$db = $_REQUEST["db"];
$wb = $_REQUEST["wb"];
$pr = $_REQUEST["pr"];
$ew = 6.112 * exp( ((17.67*$wb) / ($wb+243.5)) );
$es = 6.112 * exp( ((17.67*$db) / ($db+243.5)) );
$e0 = $pr * ($db - $wb) * 0.00066 * (1 + (0.0015 * $wb));
$e = $ew - $e0;
$rh = $e/$es;
$rhper = round($rh * 100,1);
echo "es = $es\n";
echo "ew = $ew\n";
echo "e0 = $e0\n";
echo "e = $e\n";
echo "rh = $rh, $rhper%\n";
?>
</pre>
 
and the output:

Code: Select all

es = 42.455754428627
ew = 31.674294361873
e0 = 3.45114
e = 28.223154361873
rh = 0.66476629002835, 66.5%

Re: error combining html & php on same page

Posted: Sun Sep 07, 2008 11:28 am
by starram
I m not sure, but I guess type casting may help you to get exact result.
For more details check out here and let me know if that worked.

http://in2.php.net/manual/en/language.types.float.php

If this works then let us know what u did exactly.