Multiple Submit buttons with simple codes

##Hi. My problem is like this.##


We are asked to use multiple submit buttons. It's up to us whether to use different pages or do
it in one page. We're just starting to tackle the lesson about PHP. Please help.

Codes:

`<h1>Please input the corresponding units</h1>
<form method="get">
English<input type="text" name="1st"><br>
Mathematics<input type="text" name="2nd"><br>
Science<input type="text" name="3rd"><br>
<input type="submit" value="Calculate">
<?php
$eng=$_GET['1st'];
$mat=$_GET['2nd'];
$sci=$_GET['3rd'];
$tnu=$eng+$mat+$sci; //TNU stands for total number of units
echo "Your total number of units is $tnu";
?>
Rate per Unit<input type="text" name="rpu"> 
<input type="submit" value="Calculate">
<?php
$rpu=$_GET['rpu'];
$tf=$rpu*tnu; // TF is tuition fee and in order to get that, I need to multiply the RPU and TNU

echo "Your total tuition fee is $tf";
?>`
`
//Here's the problem. Once I clicked the second submit button, it doesn't display the total 
  amount of tuition fee. It's just blank and there's an error about the $tnu. It cannot recall
  the former value of tnu. I need to fill up the inputs at the top first then click the second 
  submit button in order for it to work. I seems that whenever I clicked the first submit 
  button, it refreshes the file. Please understand. I hope you get my point.

You should set the value for your inputs using $_GET and that way the value will never be lost.
You were also missing the ‘$’ in front of ‘tnu’ in the $tf = $rpu * tnu calculation

Here’s a working example of setting the input value using $_GET
[php]

Please input the corresponding units

English
Mathematics
Science


<?php $eng = $_GET['1st']; $mat = $_GET['2nd']; $sci = $_GET['3rd']; $tnu = $eng + $mat + $sci; //TNU stands for total number of units echo "Your total number of units is $tnu"; ?>

Rate per Unit

<?php $rpu = $_GET['rpu']; $tf = $rpu * $tnu; // TF is tuition fee and in order to get that, I need to multiply the RPU and TNU
echo "Your total tuition fee is $tf";

?>
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service