dongliaojing0554 2014-10-16 16:35
浏览 7
已采纳

在具有用户输入的相同字段中显示结果[PHP]

Hello I have a simple page that allows a user to enter a binary value which is then converted into a decimal value. However, I am using two fields, one for entering the binary value, and the other to display the result in decimal.

I want to only use on field, so when the user enters the binary value, the result in decimal will appear in the same box that they entered input. Here is my current code:

<?php
if (isset($_POST['submit']))
{
    $str = $_POST['binToDec'];
    $pos = 0;
    $sum = 0;
    $tempSum = 0;
    $strLength = strlen($_POST['binToDec']) - 1;
    $powerOfTwo = 1;
    if ( $str{strLength} == 1 )
        $sum += 1;
    for ( $i = $strLength - 1; $i >=0; $i-- )
    {
        $tempSum = ($powerOfTwo *= 2);
        if ( $str{$i} == 1 )
    $sum += $tempSum;
}
}
?>
<html><body>
<form method="post" action="#">Binary value: <input name="binToDec"><br />
Result: <input value="<?php if (isset($sum)) echo $sum ?>"><br />
<input type="submit" name="submit" value="Convert">
</form>
<body></html>
  • 写回答

3条回答 默认 最新

  • dongwei2030 2014-10-16 16:39
    关注

    This this little modification will solve your issue:

    <form method="post" action="#">
    Binary value: <input name="binToDec" value="<?php if (isset($sum)) echo $sum ?>"><br />
    <input type="submit" name="submit" value="Convert">
    </form>
    

    This code is setting value on the input you've used for the POST action of the form.

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

报告相同问题?

悬赏问题

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