//获取这两个值,将它们存储在$total中并使$total = $first + $second并在 html 中回显它的最简单方法是什么?
<form action="testing123.php" method="get">
1. <input type="text" name="first"><br>
2. <input type="text" name="second"><br>
<input type="submit">
</form>
<?php
$x = $_GET["first"];
$y = $_GET["second"];
$total = $x + $y;
?>
first: <?php echo $_GET["first"]; ?><br>
second: <?php echo $_GET["second"]; ?><br>
total: <?php echo $_GET["total"]; ?>
试试这个:
<form action="testing123.php" method="get">
1. <input type="text" name="first"><br>
2. <input type="text" name="second"><br>
<input type="submit">
</form>
<?php
$x = $_GET["first"];
$y = $_GET["second"];
$total = $x + $y;
?>
first: <?php echo $_GET["first"]; ?><br>
second: <?php echo $_GET["second"]; ?><br>
total: <?php echo $total; ?>
您的$total
变量被声明为$x + $y = $total
它需要看起来像$total = $x + $y;
最后,您尝试将 $total 变量从它不在的$_GET
数组中拉出。如果您查看您的 url,您可以看到其中的$_GET
变量http://example.com/index.php?first=2&second=3
然后在脚本中将这些变量一起添加到 $total
变量中,只需 $total
即可访问。
在 testing123.php 文件中,您需要更改两行。
$x + $y = $total;
自
$total=$x+$y;
因为根据规则,右侧值被分配给左侧变量。您正在将$total
分配给$x + $y
,这毫无意义。
和改变
total: <?php echo $_GET["total"]; ?>
自
total: <?php echo $total; ?>
因为$GET
为您保存表单元素.$total是您的局部变量,它包含$x
和$y
的总和。所以你需要直接回显它。 $_GET["total"]
没有任何意义,因为我们没有任何带有"总计"名称的表单输入。