dousong2023 2015-03-13 22:03
浏览 11
已采纳

如何获取两个输入值并在php中将它们一起添加

// What is the easiest way get these two values, store them in $total and make $total = to $first + $second and echo it in the 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"]; ?>
  • 写回答

2条回答 默认 最新

  • duanjiao6730 2015-03-13 22:17
    关注

    In your testing123.php file you need to change two lines.

    $x + $y = $total;
    

    to

    $total=$x+$y;
    

    Because according to rule, right hand side value is assigned to the left variable. You are assigning $total to $x + $y ,which makes no sense.

    AND change

    total: <?php echo $_GET["total"]; ?>
    

    To

     total: <?php echo $total; ?>
    

    Because $GET holds the form elements for you.$total is your local variable which holds sum of $x and $y. So you need to echo it directly. $_GET["total"] holds no sense as we are not having any form input with 'total' name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?