【问题标题】:PHP calculator width html formPHP计算器宽度html表格
【发布时间】:2021-08-10 13:49:33
【问题描述】:

不知道为什么我的表单不起作用。

它必须计算矩形的面积。 提交后表单必须消失,并在一个句子中显示答案。

php 代码必须检查表单是否已填写并提交,然后将其隐藏并回显答案, 我只是在学习,如果问题看起来太简单了,请见谅。

<?php
if(isset($_POST['submit'])) {

if(!isset($_POST['length'], $_POST['width']))

{
  function area($a, $b) {
    $sum = $a * $b;
    echo "rectangle, with  $a cm in length and $b cm in width, area is $sum square cm.";}
    area($_POST['length'], $_POST['width']);  
} }

else { ?>
<form action="<?php $_PHP_SELF; ?>" method="POST">
        Length: <input type="text" name="length" />
        Width: <input type="text" name="width" />
        <input type="submit">
    </form>
<?php } ?>

【问题讨论】:

  • 您的代码中有几个错误。想知道您收到了哪些错误消息。
  • 你为什么不用javascript而不是php?使用php发送数据到服务器太简单了。
  • 你从来没有调用过函数 area().. 如果你只写代码来做一个函数,它不起作用,你也需要调用它......顺便问一下,你为什么使用函数,因为你只使用一次。
  • @GerarddeVisser 效果很好,很抱歉长时间回复

标签: php html forms isset


【解决方案1】:

您需要$_SERVER['PHP_SELF'] 而不是$_PHP_SELF 才能发布到当前网址。或者直接留空。

isset 的检查也不正确。

使用下面的代码,它应该可以工作:

<?php
if (!empty($_POST)) {
    if (isset($_POST['length'], $_POST['width'])) {
        function area($a, $b)
        {
            $sum = $a * $b;
            echo "rectangle, with  $a cm in length and $b cm in width, area is $sum square cm.";
        }

        area($_POST['length'], $_POST['width']);
    }
} else { ?>
    <form method="POST" enctype="multipart/form-data" action="">
        Length: <input type="text" name="length"/>
        Width: <input type="text" name="width"/>
        <input type="submit">
    </form>
<?php } ?>

【讨论】:

    猜你喜欢
    • 2014-01-01
    • 2014-05-13
    • 1970-01-01
    • 2019-08-25
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    相关资源
    最近更新 更多