I am trying to create a very simple php application that calculates TWO percentages from FOUR input fields. It's really simple, I know, but I have this big new php manual, and I can't find what I am doing wrong. My total experience with php is about 6 hours... I am a real green noob. This is the page: Resizing calculator Thanks!
<form method=get action=<?php print $PHP_SELF; ?>> Original pattern width: <input type=text name=owidth /><br> Original pattern height: <input type=text name=oheight /><br> Desired pattern width: <input type=text name=dwidth /><br> Desired pattern height: <input type=text name=dheight /><br> <p align="center"> <font size=3 color="#EEE9BF"> <input type=submit name=submit value=Calculate style="border: 1px solid black; background-color: #B7C99C; color: #FFFFFF"> </p></form> <?php { $result1 =$owidth / $dwidth *100; echo "Multiply the width by: $result1"; } { $result2 =$owidth / $dwidth *100; echo "Multiply the height by: $result2"; } ?> PHP:
<?php $result1 =(($owidth / $dwidth) *100); echo "Multiply the width by: $result1"; $result2 =(($owidth / $dwidth)*100); echo "Multiply the height by: $result2"; ?> Code (markup): Try that not tested though
First off, you should really do like name="dwidth" Secondly, when fetching the form you should really use $_GET['dwith'] and not rely on global vars. Thirdly, even though it matters less here, you should really sanitize your form input. So make sure dwith etc. is as expected, a number. Finally, add some error reporting. The PHP error says division by zero, indicating dwith = 0 or not set at all. Do stuff like, if(!is_numeric($_GET['dwidth'])) { echo 'error! not numeric!'; } Have a look what actually comes through the form before trying to fix it, so echo $_GET['dwidth'] etc. between big fat <h1>'s so it's blatantly obvious what's going on.
TOPS30, thank you. I've done and uploadded a version with names in quotation marks. As for the $_GET, I looked it up and found this. I would prefer that the name of variables etc. don't show up in the URL. Though, being a complete and utter newborn beginner, I would be elated if I only got it to work. Amazingly I understand the last part of your post " if(!is_numeric($_GET['dwidth'])) { echo 'error! not numeric!'; }" and I will try to implement such functions once I've got the basic operation working. Still not working, so I guess it wasn't the quotation marks.
You specifically made the form method="GET" hence retrieving the data via $_GET. You can change it to POST if you want. It then becomes $_POST['dwidth']. Show us your cleaned up code. When you paste chunks of PHP here, use [php]code here[/php]
<form method="post" action=<?php print $PHP_SELF; ?>> Original pattern width: <input type="text" name="owidth" /><br> Original pattern height: <input type="text" name="oheight" /><br> Desired pattern width: <input type="text" name="dwidth" /><br> Desired pattern height: <input type="text" name="dheight" /><br> <p align="center"> <font size=3 color="#EEE9BF"> <input type=submit name=submit value=Calculate style="border: 1px solid black; background-color: #B7C99C; color: #FFFFFF"> </p></form> <?php $result1 =(($owidth / $dwidth) *100); echo "Multiply the width by: $result1"; $result2 =(($owidth / $dwidth)*100); echo "Multiply the height by: $result2"; ?> PHP:
<form method="post" action=<?php print $PHP_SELF; ?>> Original pattern width: <input type="text" name="owidth" /><br> Original pattern height: <input type="text" name="oheight" /><br> Desired pattern width: <input type="text" name="dwidth" /><br> Desired pattern height: <input type="text" name="dheight" /><br> <p align="center"> <font size=3 color="#EEE9BF"> <input type=submit name=submit value=Calculate style="border: 1px solid black; background-color: #B7C99C; color: #FFFFFF"> </p></form> <?php if (isset($submit)) { //so it doesn't load if not submitted $result1 =(($_POST['owidth'] / $_POST['dwidth']) * 100); echo "Multiply the width by:" . $result1; $result2 =(($_POST['owidth'] / $_POST['dwidth']) * 100); echo "Multiply the height by:" . $result2; }?> PHP: Try that and paste any errors, if any. BTW result1 and 2 will be the exact same, so a lot of the code is rather pointless...
This gives a "parsing error." I did correct the formula a bit: <?phpif (isset($submit)) { //so it doesn't load if not submitted$result1 =(($_POST['owidth'] / $_POST['dwidth']) * 100); echo "Multiply the width by:" . $result1; $result2 =(($_POST['owidth'] / $_POST['dwidth']) * 100); echo "Multiply the height by:" . $result2;}?> PHP:
<form method="post" action=<?php print $PHP_SELF; ?>> Original pattern width: <input type="text" name="owidth" /><br> Original pattern height: <input type="text" name="oheight" /><br> Desired pattern width: <input type="text" name="dwidth" /><br> Desired pattern height: <input type="text" name="dheight" /><br> <p align="center"> <font size=3 color="#EEE9BF"> <input type=submit name=submit value=Calculate style="border: 1px solid black; background-color: #B7C99C; color: #FFFFFF"> </p></form> <?php if($submit) { $result1 =(($_POST['owidth']/ $_POST['owidth']) *100); echo "Multiply the width by: $result1 <br />"; $result2 =(($_POST['dwidth']/ $_POST['dwidth']) *100); echo "Multiply the height by: $result2"; } ?> PHP: Should work
Gekkie's code doesn't give a parsing error. However, upon "submit" the page reloads and no solutions are given to the calculations.
Within the if($submit) add some debugging like: echo '<h2>Dwidth:' . $_POST['dwidth'] . '</h2>'; PHP: And then see whether that shows. That way you can reverse debug, bottom up, your code. I'd also make it type="submit" name="submit". With neat "s.
$result1 =(($_POST['owidth']/ $_POST['owidth']) *100); echo "Multiply the width by: $result1 <br />"; $result2 =(($_POST['dwidth']/ $_POST['dwidth']) *100); echo "Multiply the height by: $result2"; PHP: Stupid me i divide owidth with owidth you should replace does words with the ones you wanna divide.
I found the problem... Look at the page source, $PHP_SELF isn't actually put in the form. Make it <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> PHP:
Yes, I changed it to: <?php if($submit) { $result1 =(($_POST['owidth']/ $_POST['dwidth']) *100); echo "Multiply the width by: $result1 <br />"; $result2 =(($_POST['oheight']/ $_POST['dheight']) *100); echo "Multiply the height by: $result2"; }?> PHP: Still not calculating...
Also replace the if ($submit with this: if($_POST["submit"]){ To be sure. Some server don't have global vars enabled.