dtuct88226 2016-06-23 14:32
浏览 39

乘以$ _POST

I am trying to make a simple program where the user can multiply their inputs. But it always return a 0.

I already did:

$price = (int)$_POST['txtPrice'];

$quan = (int)$_POST['quantity'];


$ans = $quan * $price;
echo "$ans";

But returns 0 always. This is the page where all the textboxes came from:

echo "<tr>";
echo "<td><input type='text' name='quantity' placeholder='How Many?'></td>";
echo "<td><input type='text' name='txtName' value='$name'></td>";
echo "<td><input type='text' name='txtPrice' value='$price'></td>";
  • 写回答

1条回答 默认 最新

  • douyou7072 2016-06-23 14:50
    关注

    Using your same code and just adding the form tag and a submit works for me:

    <?php
    
    if(!empty($_POST)) {
        print_r($_POST);
        $price = (int)$_POST['txtPrice'];
        $quan = (int)$_POST['quantity'];
    
        $ans = $quan * $price;
        echo "$ans<br>";
    }
    
    echo "<tr>";
    echo "<form action='index.php' method='post'>";
    echo "<td><input type='text' name='quantity' placeholder='How Many?'>    </td>";
    echo "<td><input type='text' name='txtName' value='$name'></td>";
    echo "<td><input type='text' name='txtPrice' value='$price'></td>";
    echo "<input type='submit'>";
    echo "</form>";
    

    Execute it with php -S 127.0.0.1 index.php, $ans contains the value from the multiplication.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度