...

View Full Version : Need Help with PHP Calculate Program.



splendid
05-02-2009, 04:11 AM
I wrote a PHP script to calculate two numbers. The form displays correctly and when it tries to calculate, for some reason it just see's the first number. Attached below is my code. Can anyone tell me what I am doing wrong?

Thanks,

Rob :)



<HTML>
<H1><B>The Calculator Application</B></H1>
</HTML>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="first_number" value=""/> + <input type="text" name="seccond_number" value="" /> = <?=$number?><br />

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

</form>

<?php

if (isset ($_POST ['add'])) {
$first_number = $_POST['first_number'];
$second_number = $_POST['second_number'];

$number = $first_number + $second_number;
echo $_POST['number'];
echo "<br>";
echo $number;

}
?>

Fou-Lu
05-02-2009, 04:15 AM
I wrote a PHP script to calculate two numbers. The form displays correctly and when it tries to calculate, for some reason it just see's the first number. Attached below is my code. Can anyone tell me what I am doing wrong?

Thanks,

Rob :)



<HTML>
<H1><B>The Calculator Application</B></H1>
</HTML>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="first_number" value=""/> + <input type="text" name="seccond_number" value="" /> = <?=$number?><br />

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

</form>

<?php

if (isset ($_POST ['add'])) {
$first_number = $_POST['first_number'];
$second_number = $_POST['second_number'];

$number = $first_number + $second_number;
echo $_POST['number'];
echo "<br>";
echo $number;

}
?>

<input type="text" name="seccond_number" value="" /> versus $second_number = $_POST['second_number'];. One of those two has to change (chances are its you're html input you're going to change).

Also, <?= is not recommended since is uses an echo off of a short_tags directive which is an option that can be disabled. PHP_SELF is also XSS exploitable, so that should be changed. In this case, try using <?php echo $_SERVER['SCRIPT_NAME'];?> instead of <?=$_SERVER['PHP_SELF']?>

venegal
05-02-2009, 04:17 AM
Just browsing through, that typo might be your problem:
name="seccond_number"

splendid
05-02-2009, 04:23 AM
Folks,

I can't thank you enough. I did not see the typo because it wrapped to another line. I used all of your suggestions, and again really appreciate your help! Everything works perfectly now!!!


Rob:thumbsup:

randomTraveler
05-02-2009, 05:50 AM
I find that it is often easier to use the extract method, as it prevents typos when using super global arrays.


rather than doing this....

$first_number = $_POST['first_number'];
$second_number = $_POST['second_number'];

you can replace with
extract($_POST) ;

By extracting the $_POST superglobal array, you can simply write $first_number, and $second_number , and do not need to define them on a seperate line of code, as $first_name is defined from the form's first_name field....

It's less typing too...

Fou-Lu
05-02-2009, 06:33 AM
Thats a great way to have you're variables overwritten.

Default handling of extract is to replace current variables. Extract may be simplier to use, but it is only slightly better than using register_globals. Extract should NEVER be used on any user submitted data. Ever.

sea4me
05-02-2009, 03:23 PM
Thats a great way to have you're variables overwritten.

Default handling of extract is to replace current variables. Extract may be simplier to use, but it is only slightly better than using register_globals. Extract should NEVER be used on any user submitted data. Ever.

I learned something today...

I didn't think of overwritting the vars intill you mentioned it...

(I was gonna use extract forever without you telling how bad it was)

Nicely said!!! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum