Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can someone write a code for me???

    I need a calculator to calculate numbres that I pass to variables,
    So far I wrote this script that contains, also html code:
    if anyone can help me, tell me where am I doing wrong:

    <?

    $a=$_POST['numer1'];
    $b=$_POST['numer2'];
    $c=$_POST['operator'];
    $d=$_POST['equall'];

    $rez="";

    if(isset($d)) {
    switch($c) {
    case "+":
    $rez = ($a + $b);
    break;
    case "-":
    $rez = ($a - $b);
    break;
    case "*":
    $rez = ($a * $b);
    break;
    case "/":
    $rez = ($a / $b);
    break;
    }
    }









    ?>

    <!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>Untitled Document</title>
    </head>

    <body>
    <table border="1" width="300" height="300">
    <form method="post" action="">
    <tr>
    <td colspan="2">
    <table border="1">
    <tr>
    <td width="75"><? echo $a ?></td>
    <td width="75"><? echo $b ?></td>
    <td width="75"><? echo $c ?></td>
    <td width="75"><? echo $rez ?></td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td valign="top">
    <table border="1">
    <tr>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="9"/></td>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="8"/></td>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="7"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="9"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="8"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="7"/></td>
    </tr>
    <tr>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="6"/></td>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="5"/></td>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="4"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="6"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="5"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="4"/></td>
    </tr>
    <tr>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="3"/></td>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="2"/></td>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="1"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="3"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="2"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="1"/></td>
    </tr>
    <tr>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="0"/></td>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="0"/></td>
    <td width="45" height="30"><input type="submit" name="numer1[]" value="0"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="0"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="0"/></td>
    <td width="45" height="30"><input type="submit" name="numer2[]" value="0"/></td>
    </tr>
    </table>
    </td>
    <td valign="top">
    <table border="1">
    <tr>
    <td width="74"><input type="submit" name="operator[]" value="+"/></td>
    </tr>
    <tr>
    <td width="74"><input type="submit" name="operator[]" value="-"/></td>
    </tr>
    <tr>
    <td width="74"><input type="submit" name="operator[]" value="*"/></td>
    </tr>
    <tr>
    <td width="74"><input type="submit" name="operator[]" value="/"/></td>
    </tr>
    <tr>
    <td width="74"><input type="submit" name="equall[]" value="="/></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    </body>
    </html>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    *sniff sniff*

    Smells like homework....

    I can help you with a specific issue but if you just want work done then I'll send you a rate sheet.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Works for me. Do you get an error?

    I'd imagen its cause the 'operator' is tripping the code up. You will need to encase it in quotes...

    change the collection of vars from what you have to this below...
    PHP Code:
    $a="".$_POST['numer1']."";
    $b="".$_POST['numer2']."";
    $c="".$_POST['operator']."";
    $d="".$_POST['equall'].""
    Best of luck

  • #4
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It still doesn't work(calculator),
    thou I don't get any errors...

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Looking at it... the PHP is fine. The problem lies in the HTML form code.

    You have input tags that all have [] in the name, that is no good. In addition your form has no action and you have many fields with indentical names. Looks like you either need to tidy that up and put in a form action, or put onClick = form.submit buisness on the buttons.

    This should be addressed in another forum I believe.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •