输入值时禁用某个文本框?

时间:2018-01-26 16:52:40

标签: php html

我正在制作类似于This Calculator

的计算器

但是我没有使用Javascript而只使用HTML和PHP而不使用任何脚本,甚至不使用CSS。

发生的事情是有4个文本框,取决于下拉菜单中的选项,每次其中一个留空,因为答案将输出应该在该文本框中但我回应回答并希望禁用当时不需要其值的框。

任何帮助都会受到赞赏,我知道它是一个非常糟糕的代码,但暂时我只想添加我所描述的内容:

<!DOCTYPE html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Aluminum Pipe - Pressure Rating Calculation </title>
</head>

<body>
    <form action="AlPipe.php" method="post">
        <div id="new" align="center">

            <h2 align="center">Ordering Decimals from Least to Greatest</h2>
            <select name="new" align="center">
  <option align="center" value="P">Pressure Rating (PR)</option>
  <option align="center" value="S">Allowable Stress (S) </option>
  <option align="center" value="T">Wall Thickness(T)</option>
  <option align="center" value="D">Pipe Outside Diameter(D)</option>
</select>
            <p align="center">Allowable Stress(S) =
                <input type="text" name="AL" size="5" />
            </p>
            <p align="center"> Wall Thickness(t) =
                <input type="text" name="WT" size="5" />
            </p>
            <p align="center">Pipe Outside Diameter(D) =
                <input type="text" name="POD" size="5"/>
            </p>
            <p align="center">Pressure Rating(PR) =
                <input type="text" name="PR" size="5" />
            </p>
            <div align="center">
                <input type=submit value=Calculate name="Calculate">
                <input type=reset value=Reset>
<?php
formula();

            function formula()
            {
            if(isset($_POST['Calculate']))
            {
                $S=$_POST['AL'];
                $T=$_POST['WT'];
                $D=$_POST['POD'];
                $P=$_POST['PR'];
                $cal=$_POST['new'];

            }
if($cal=='P')
{
$PR1 = (2 * $S * $T );
$PR2 = $D;
$PR3 = $PR1 / $PR2;
echo "\n Pressure Rating is : ".$PR3;

}
elseif($cal=='S')
{
$S1= ($P * $D)  ;
$S2= ($T * 2);
$S3= $S1 / $S2;
echo "\n Allowable Stress is : ".$S3;
}
elseif($cal=='T')
{
$T1 = $P * $D  ;
$T2 = 2 * $S;
$T3 = $T1 / $T2;
echo "\nWall Thickness is : ".$T3;
}
elseif($cal=='D')
{
$D1 = 2 * $S * $T / $P ;
echo "\nDiameter is : ".$D1;
}
else
{
echo "Invalid Values";
}

                }
?>
            </div>
    </form>
    </div>
    </div>
</body>

1 个答案:

答案 0 :(得分:0)

提交后或根据下拉菜单是否需要禁用该字段?无论哪种方式,您可以使表单提交到PHP,然后使用类似下面的内容将禁用的关键字注入您的计算器,例如,您可以有一个下拉框,旁边有一个更新按钮,它与下面的内容相同除了它使用下拉列表中的帖子数据来确定要禁用的内容。

<!-- language: lang-php -->
<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>
        Aluminum Pipe - Pressure Rating Calculation 
    </title>
</head>
<body>
    <form method="post">
        <div id="new" align="center">
            <h2 align="center">
                Ordering Decimals from Least to Greatest
            </h2>
            <select name="new" align="center">
                <option align="center" value="P">Pressure Rating (PR)</option>
                <option align="center" value="S">Allowable Stress (S) </option>
                <option align="center" value="T">Wall Thickness(T)</option>
                <option align="center" value="D">Pipe Outside Diameter(D)</option>
            </select>
            <p align="center">
                Allowable Stress(S) =
                <input type="text" name="AL" size="5" <?= (isset($_POST['Calculate']) && (isset($_POST['AL']) && $_POST['WT'] == '')) ? 'disabled' : ''; ?> />
            </p>
            <p align="center">
                Wall Thickness(t) =
                <input type="text" name="WT" size="5" <?= (isset($_POST['Calculate']) && (isset($_POST['WT']) && $_POST['WT'] == '')) ? 'disabled' : ''; ?> />
            </p>
            <p align="center">
                Pipe Outside Diameter(D) =
                <input type="text" name="POD" size="5" <?= (isset($_POST['Calculate']) && (isset($_POST['POD']) && $_POST['POD'] == '')) ? 'disabled' : ''; ?> />
            </p>
            <p align="center">
                Pressure Rating(PR) =
                <input type="text" name="PR" size="5" <?= (isset($_POST['Calculate']) && (isset($_POST['PR']) && $_POST['PR'] == '')) ? 'disabled' : ''; ?> />
            </p>
            <div align="center">
                <input type=submit value=Calculate name="Calculate">
                <input type=reset value=Reset>
            </div>
        </form>
    </div>
</div>
</body>
<?php
formula();
function formula()
{
    if(isset($_POST['Calculate']))
    {
        $S=$_POST['AL'];
        $T=$_POST['WT'];
        $D=$_POST['POD'];
        $P=$_POST['PR'];
        $cal=$_POST['new'];
    }
    if($cal=='P')
    {
        $PR1 = (2 * $S * $T );
        $PR2 = $D;
        $PR3 = $PR1 / $PR2;
        echo "\n Pressure Rating is : ".$PR3;
    }
    elseif($cal=='S')
    {
        $S1= ($P * $D)  ;
        $S2= ($T * 2);
        $S3= $S1 / $S2;
        echo "\n Allowable Stress is : ".$S3;
    }
    elseif($cal=='T')
    {
        $T1 = $P * $D  ;
        $T2 = 2 * $S;
        $T3 = $T1 / $T2;
        echo "\nWall Thickness is : ".$T3;
    }
    elseif($cal=='D')
    {
        $D1 = 2 * $S * $T / $P ;
        echo "\nDiameter is : ".$D1;
    }
    else
    {
        echo "Invalid Values";
    }
}
?>