doubianxian6557 2014-05-14 09:58
浏览 25

将变量从一种形式用于另一种形式时出错

In PHP I would like to use the $num value that is entered in the first form and execute a pow() function with $num and another variable, named $rate, which I enter in the second form.

<body>
<form action="" method="post">
    Enter number: <input type="number" name="num" id="num"/>
    <input type="submit" name="next" value="Next"/><br>
</form>
<?php   if( isset($_POST['next']) ) : $num = $_POST['num']; ?>
        Entered number: <input type="text" name="entered" id="entered" value=" <?=$num ?> "/>
<?php   endif; ?>

<form action="" method="post">
    Enter rate: <input type="number" name="rate" id="rate"/>
    <input type="submit" name="calculate" value="Calculate"/>
</form>
<?php   if( isset($_POST['calculate']) ) : $rate = $_POST['rate'];
            if( $rate >=1 && $rate <=10 ) : echo pow($num, $rate); endif;
        endif;
?>
</body

When the code is executed it shows me:

Notice: Undefined variable: num in ... on line ...
0

What I need to add in the code to recognize the $num variable and execute right the pow()?

NB: Don't pay attention to the 'entered' input - it's just for a test.

  • 写回答

1条回答 默认 最新

  • doucha4054 2014-05-15 10:15
    关注

    This is the solution I found yesterday and it works pretty well. Hope to be well understandable.

    <?php   if( empty($_POST) ): ?>
    <form action="" method="post">
        Enter number: <input type="number" name="num" id="num"/>
        <input type="submit" name="next" value="Next"/><br>
    </form>
    <?php   endif; ?>
    
    <?php   if( isset($_POST['next']) ) : $num = $_POST['num']; ?>
    <form action="" method="post">
        Entered number: <input type="text" name="entered" id="entered" value=" <?=$num ?> "/><br>
        Enter rate: <input type="number" name="rate" id="rate"/>
        <input type="submit" name="calculate" value="Calculate"/>
    </form>
    <?php   endif;
    
        if( isset($_POST['calculate']) ) : $rate = $_POST['rate'];
            $num = $_POST['entered']; 
            if( $rate >=1 && $rate <=10 ) : echo pow($num, $rate); 
            else: echo 'Enter rate between 1 and 10' . '<form> <input type="submit" value="Back" onClick="history.go(-1);return TRUE;"> </form>';
            endif;
        endif;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离