What did I do wrong?

Discussion in 'PHP' started by Geloo, Jun 22, 2010.

  1. #1
    Hallo, I'm very new to php so I started off with creating a simple calculator now every time I'm testing it an error occurs that something is wrong on line 2 when I try to calculate something can somebody help me out?;)

    HTML CODE:

    <!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>Calculator</title>
    </head>
    
    <body><form method="post" action="calculate.php">
    <p>Value 1.<input type="text" name="value1" width="15" height="8" /></p>
    <p>Value 2.<input type="text" name="value2" height="8" width="15" /></p>
    <p>Calculation:<br>
    
    <input type="radio" name="calc" value="addition"/>Addition<br>
    <input type="radio" name="calc" value="substract"/> Substract<br>
    <input type="radio" name="calc" value="multiply"/>Multiply<br>
    <input type="radio" name="calc" value="divide"/>Divide</p>
    
    <input type="submit" name="submit" value="calculate"/>
     
     
    </form>
    
    </body>
    </html>
    HTML:
    PHP CODE:

    <?php
    if ((if $_POST [value1] =="" ) || (if $_POST[value2] =="") || ($_POST[calc] =="")) {
       header(Location: "index.html");
       exit;
    )
    
    if ( $_POST[calc] =="addidtion")
    { $result ==($_POST[value1]) + ($_POST[value2]};
    else if ($_POST[calc] =="substract")
    { $result == $_POST[value1] - $_POST[value2]};
    else if ($_POST[calc] =="multiply"}
    { $result == $_POST[value1] * $_POST[value2]};
    else if($_POST[calc] =="divide")
    { $result == $_POST[value1] / $_POST[valu2]};
    
    echo"<title>Calculation:</title>";
    echo"<br>The result of the calculation is: $result"; 
    
    
    
    ?>
    
    Code (markup):
     
    Geloo, Jun 22, 2010 IP
  2. bvraghav

    bvraghav Member

    Messages:
    123
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    it happens sometimes...

    this was what you did wrong
    if ((if $_POST [value1] =="" ) || (if $_POST[value2] =="") || ($_POST[calc] =="")) {
    
    PHP:
    and this is how it looks after correction:
    if (($_POST [value1] =="" ) || ($_POST[value2] =="") || ($_POST[calc] =="")) {
    PHP:
    there were unnecessary if's

    cheers...
     
    bvraghav, Jun 22, 2010 IP
  3. Geloo

    Geloo Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hahaha oke, thanks but you say they are unnecessary that means it shouldn't give an error when I still use them?:p
     
    Geloo, Jun 22, 2010 IP
  4. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    { $result ==($_POST[value1]) + ($_POST[value2]};

    You really want to be santizing this and you're missing a closing ) it seems plus consistent spacing will make it easier to look at and go through
     
    krsix, Jun 22, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    This should get you started:

    <form method="post">
    <p>Value 1.<input type="text" name="value1" width="15" height="8" /></p>
    <p>Value 2.<input type="text" name="value2" height="8" width="15" /></p>
    <p>Calculation:<br>
    
    <input type="radio" name="calc" value="addition"/>Addition<br>
    <input type="radio" name="calc" value="subtract"/>Subtract<br>
    <input type="radio" name="calc" value="multiply"/>Multiply<br>
    <input type="radio" name="calc" value="divide"/>Divide</p>
    
    <input type="submit" name="submit" value="calculate" />
     
    </form>
    
    <?php
    
    if (isset($_POST['submit']) && isset($_POST['calc'])) {
    
    if (!is_numeric($_POST['value1']) || !is_numeric($_POST['value2'])) {
    
    echo "Please make sure the value's are filled in and are numbers.";
    
    } else {
    
    $calculation = $_POST['calc'];
    $value_one = intval($_POST['value1']);
    $value_two = intval($_POST['value2']);
    
    if ($calculation == "addition") { 
    
    $result = $value_one + $value_two;
    
    } elseif ($calculation == "subtract") { 
    
    $result = $value_one - $value_two;
    
    } elseif ($calculation == "multiply") { 
    
    $result = $value_one * $value_two;
    
    } elseif ($calculation == "divide") { 
    
    $result = $value_one / $value_two;
    
    }
    
    echo "The result of the calculation is: {$result}"; 
    
    }
    }
    
    ?>
    PHP:
    Also look into:

    http://www.tizag.com/phpT/operators.php
     
    danx10, Jun 22, 2010 IP
  6. Geloo

    Geloo Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you very much could I'm soon gonna try to work with the isset function
     
    Geloo, Jun 22, 2010 IP