#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Problem with php code simple calculater


    Hello,

    I have a problem writing a code in one file. It's a drop list having a different operation ( - + / * ) I have to select one of these and before that there are two text field to write two number. assuming I choosed ( * ) and write in number1: 12 and number2: 3

    the output should be :

    12 * 3 = 36

    and it should appear in the same page (calculator page)

    here is a picture>

    im42.gulfup.com/0GraB.jpg

    this is my code:
    PHP Code:
    <html> <head><title> test </title> </head>  <body> <h1> Enter Comment </h1> <form action="new.php" method="get">  Number1: <input type="text" name="num1"> Number2: <input type="text" name="num2">     <select name="cal">  <option> + </option> <option> - </option> <option> / </option> <option> * </option>  </select>  <input type="submit" value="Do Math">      <?php $cal "";      if ( $_GET["cal"] == + )     echo $_GET["num1"] ."+"$_GET["num2"] ."="$_GET["num1"] + $_GET["num2"];          if ( $_GET["cal"] == - )     echo $_GET["num1"] - $_GET["num2"];          if ( $_GET["cal"] == / )     echo $_GET["num1"] / $_GET["num2"];          if ( $_GET["cal"] == * )     echo $_GET["num1"] * $_GET["num2"];       setcookie ("cal" "" time()+3600);      echo      ?>    </form> </body>  </html>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    so what is the problem? Does PHP get the math wrong? Do you get an error message? A blank screen? Does your CPU melt as soon as you execute the script?

    Contrary to popular belief, we're not psychic. We cannot read your thoughts or access your PC through telepathy. This means you need to actually tell us what's wrong. With words and stuff.

    From what I can tell, you have several syntax error due to unquoted strings (like in $_GET["cal"] == +). And there's an empty echo at the end. But if those are the errors you mean I don't know.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo