本帖最後由 guo.cane 於 2013-10-31 19:05 編輯
1.php- <html>
- <head>
- <title>計算機</title>
- <meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
- </head>
- <body>
- <form action="2.php" method="get"><!-- action="處理的網址" method="get:網址傳值/post:封包傳值" -->
- <input type="text" name="number1" size="5" maxlength="5" />
- <select name="operate">
- <option value="+">+</option>
- <option value="-">-</option>
- <option value="*">*</option>
- <option value="/">/</option>
- <option value="%">%</option><!-- 餘數運算子 -->
- </select>
- <input type="text" name="number2" size="5" maxlength="5" /> =
- <input type="submit" name="cal" value="計算" />
- </form>
- </body>
- </html>
複製代碼 2.php- <?php
- header('Content-Type:text/html; charset=utf-8'); //防止亂碼(PHP)
- $number1 = $_GET['number1']; //$_GET 一律大寫
- $operate = $_GET['operate']; //$_GET 一律大寫
- $number2 = $_GET['number2']; //$_GET 一律大寫
-
- //eval("echo \"$number1\"$operate\"$number2\";");
-
- /*
- echo $number1.'<br />';
- echo $operate.'<br />';
- echo $number2.'<br />';
- */
- switch($operate){
- case '+':
- echo '您的答案是 '.($number1 + $number2).'<br />';
- break;
- case '-':
- echo '您的答案是 '.($number1 - $number2).'<br />';
- break;
- case '*':
- echo '您的答案是 '.($number1 * $number2).'<br />';
- break;
- case '/':
- echo '您的答案是 '.($number1 / $number2).'<br />';
- break;
- case '%':
- echo '您的答案是 '.($number1 % $number2).'<br />';
- break;
- }
- ?>
複製代碼 |