code not running properly, need guidance...

Discussion in 'PHP' started by prashant50388, Nov 6, 2011.

  1. #1
    Hello Everyone, I am new to PHP and trying to run this very simple code but it is giving me some absurd output. Details are here:

    OS: Windows 7 home premium
    PHP version: 5.3.8
    Web server: IIS on windows 7

    ****************************************************************************
    HTML file (calculate_form.html):

    <html>
    <head>
    <title>Calculation Form</title>
    </head>
    <body>
    <form method="post" action="calculate.php">
    <p>Value 1: <input type="text" name="val1" size="10"></p>
    <p>Value 2: <input type="text" name="val2" size="10"></p>
    <p>Calculation:<br>
    <input type="radio" name="calc" value="add"> add<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>
    <p><input type="submit" name="submit" value="Calculate"></p>
    </body>
    </html>

    *********************************************************************************

    PHP file (calculate.php):

    <?php
    if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] =="")) {
    header("Location: calculate_form.html");
    exit;
    }
    if ($_POST[calc] == "add") {
    $result = $_POST[val1] + $_POST[val2];
    } else if ($_POST[calc] == "subtract") {
    $result = $_POST[val1] - $_POST[val2];
    } else if ($_POST[calc] == "multiply") {
    $result = $_POST[val1] * $_POST[val2];
    } else if ($_POST[calc] == "divide") {
    $result = $_POST[val1] / $_POST[val2];
    }
    echo <title>Calculation Result</title>;
    echo <br>The result of the calculation is: $result;
    echo <p><a href=\calculate_forum.html\ target=\_self\> Another</a>;
    ?>

    **********************************************************************************

    The output i am getting on the browser is:

    Calculation Result; echo
    The result of the calculation is: $result; echo
    Another; ?>

    **********************************************************************************
     
    Solved! View solution.
    prashant50388, Nov 6, 2011 IP
  2. #2
    Try this:
    <?php
    if ($_POST[val1] == "" || $_POST[val2] == "" || $_POST[calc] =="") {
        header("Location: calculate_form.html");
        exit;
    }
    if ($_POST[calc] == "add") {
        $result = $_POST[val1] + $_POST[val2];
    } else if ($_POST[calc] == "subtract") {
        $result = $_POST[val1] - $_POST[val2];
    } else if ($_POST[calc] == "multiply") {
        $result = $_POST[val1] * $_POST[val2];
    } else if ($_POST[calc] == "divide") {
        $result = $_POST[val1] / $_POST[val2];
    }
    echo '<title>Calculation Result</title>';
    echo '<br>The result of the calculation is: '.$result;
    echo '<p><a href="calculate_forum.html">Another</a>';
    ?>
    PHP:
     
    ChefGaby, Nov 6, 2011 IP
  3. prashant50388

    prashant50388 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you!!
    its working
     
    prashant50388, Nov 6, 2011 IP