这是一个创建于 2062 天前的主题,其中的信息可能已经有所发展或是发生改变。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>a small calculate</title>
</head>
<body>
<form>
<input type="text" name="num1" placeholder="number 1">
<input type="text" name="num2" placeholder="number 2">
<select name="operator">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<br>
<button name="submit" value="good submit" type="submit" >Calculate</button>
</form>
<p>the answer is:</p>
<?php
if(isset($_GET['submit'])){
$result1 = $_GET('num1');
$result2 = $_GET('num2');
$operator = $_GET('operator');
switch($operator){
case "None":
echo "Error,you need select a method.";
break;
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
</body>
</html>
运行后测试:文本框输入值后,点击 calculate 按钮返回错误:
Fatal error: Function name must be a string in /Applications/XAMPP/xamppfiles/htdocs/calculate.php on line 29
请问如何解决呢?
代码片段:
28 行 if(isset($_GET['submit'])){
29 行 $result1 = $_GET('num1');
30 行 $result2 = $_GET('num2');
31 行 $operator = $_GET('operator');
10 条回复 • 2019-02-08 17:54:42 +08:00
|
|
1
898684526 2019-02-06 19:45:04 +08:00
获取 get 参数应该是 [] 而不是()
|
|
|
2
caola 2019-02-06 19:45:06 +08:00
$_GET 获取的数据并不存在, 先判断是否存在: $result1 = $_GET('num1') ?? 0;
|
|
|
3
898684526 2019-02-06 19:45:36 +08:00
你从 29 行 $_GET('num1') 应该是 $_GET['num1'];
|
|
|
4
caola 2019-02-06 19:46:13 +08:00
确实是方括号问题(上面我直接复制了)
|
|
|
5
clf 2019-02-06 19:46:15 +08:00
$_GET['xxx'] 不是 $_GET('xxx')
|
|
|
6
githere 2019-02-06 19:57:12 +08:00
|
|
|
7
Hardrain 2019-02-06 20:59:23 +08:00
var_dump($_GET)可知其为数组,应使用$_GET['parameter']
$_POST $_SERVER $_COOKIE 等也是数组
|
|
|
8
orqzsf1 2019-02-06 21:02:32 +08:00
<select> <option value ="volvo">Volvo</option> <option value ="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select>
|
|
|
10
bzj 2019-02-08 17:54:42 +08:00
楼主把这里当百度知道了,基础不好不去学习,跑来这里问,习惯不好
|