0

I'm making a calculator similar to This Calculator

But instead of using Javascript I'm using only HTML and PHP without any kind of scripting, not even CSS.

What happened is that there are 4 text boxes and depending on the choice from the dropdown menu, every time one of those is left empty because the answer will be output which should be in that textbox but I'm echoing the answer and want to disable the box whose value is not needed at that time.

Any help would be appreciated and I know it's a very lousy code but for the time being I just want to add what I described:

<!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>
phuclv
  • 37,963
  • 15
  • 156
  • 475
hacker red
  • 15
  • 7
  • 1
    You can't do that in PHP. It runs on the server after the form is submitted, it can't make real-time changes on the client. – Barmar Jan 26 '18 at 16:56
  • 2
    this has to be done in javascript – coder Jan 26 '18 at 16:57
  • @Barmar this is an assignment through which my teacher is trying to figure out how soon you can make someone kill themselves by frustrating them.And it has to be done in Javascript but somehow I did it in PHP and its working on xampp the last problem I couldn't figure out, I have presented it to you guys.And coder thanks for stating the obvious – hacker red Jan 26 '18 at 17:05
  • The calculations can obviously be done in PHP, but disabling a field has to be done in Javascript. – Barmar Jan 26 '18 at 17:06
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Rotimi Jan 26 '18 at 17:09
  • Well my dilemma here is that I can't for no reason use anything except PHP and HTML so *feels bad man* – hacker red Jan 26 '18 at 17:16
  • 1
    fix your indentation first. It's terrible. Without proper indentation how can you do programming? And I couldn't understand what you said about the textboxes – phuclv Apr 14 '19 at 10:18
  • Were you to fix your indentation, you'd notice that you have a closing tag which does not have a matching opening tag! – Compo Apr 14 '19 at 12:26

1 Answers1

0

Does the field need to be disabled after the submit or based on the drop down? Either way you can make the form submit to php and then use something like the following to inject the disabled keyword into your calculator for example you could have a drop down box that has an update button next to it that does the same thing as what follows with the exception it uses the post data from the dropdown to determine what to disable.

<!-- 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";
    }
}
?>
phuclv
  • 37,963
  • 15
  • 156
  • 475
KaffineAddict
  • 436
  • 2
  • 11