HELP a beginner... simple question!

Discussion in 'PHP' started by helleborine, Oct 7, 2006.

  1. #1
    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!
     
    helleborine, Oct 7, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Without seeing code we can't help.
     
    T0PS3O, Oct 7, 2006 IP
  3. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #3
    <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:
     
    helleborine, Oct 7, 2006 IP
  4. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #4
    I should probably be very embarassed... I am sure my errors are obvious!
     
    helleborine, Oct 7, 2006 IP
    Colleen likes this.
  5. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #5
    
    <?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
     
    Big 'G', Oct 7, 2006 IP
  6. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #6
    Big G, I uploaded it... not working!
     
    helleborine, Oct 7, 2006 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    T0PS3O, Oct 7, 2006 IP
  8. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #8
    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.
     
    helleborine, Oct 7, 2006 IP
  9. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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]
     
    T0PS3O, Oct 7, 2006 IP
  10. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #10
    <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:
     
    helleborine, Oct 7, 2006 IP
  11. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #11
    <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...
     
    T0PS3O, Oct 7, 2006 IP
  12. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #12
    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:
     
    helleborine, Oct 7, 2006 IP
  13. Gekkie

    Gekkie Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    
    
    <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, Oct 7, 2006 IP
  14. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #14
    If there's a parsing error, please show us and the corresponding line.
     
    T0PS3O, Oct 7, 2006 IP
  15. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #15
    Gekkie's code doesn't give a parsing error.

    However, upon "submit" the page reloads and no solutions are given to the calculations.
     
    helleborine, Oct 7, 2006 IP
  16. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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.
     
    T0PS3O, Oct 7, 2006 IP
  17. Gekkie

    Gekkie Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #17
    
    
    $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.
     
    Gekkie, Oct 7, 2006 IP
  18. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #18
    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:
     
    T0PS3O, Oct 7, 2006 IP
  19. helleborine

    helleborine Well-Known Member

    Messages:
    915
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    120
    #19
    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...
     
    helleborine, Oct 7, 2006 IP
  20. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Also replace the if ($submit with this:

    if($_POST["submit"]){

    To be sure. Some server don't have global vars enabled.
     
    T0PS3O, Oct 7, 2006 IP