Simple multiplication script for form

I’m totally new at this, and trying to get started. Can anyone help with a script I can put with a form where input will be
“Hectareas” and “Kilos”
formula is simply “Hectareas” * “Kilos” /100
output goes to a field called “Dosis” and
gets divided by 20 and echoed to a field called “Bidones”

Then I’ll try to complicate it for further output.

thanks in advance


<?php if (empty($_POST)) { ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
	<input type="text" name="hectares">
	<input type="text" name="kilos">
	<input type="submit" value=" OK ">

<?php } else { ?>

Dosis: <?php echo $_POST['hectares'] * $_POST['kilos'] / 100; ?><br />
Bidones: <?php echo $_POST['hectares'] * $_POST['kilos'] / 2000; ?><br />

<?php } ?>

Here’s a quick example. You could (and should) do more user input validation but it should give you an idea:


//initialise errors array
$errors = array();
//was the form submitted?
  //was a valid number entered for hectareas?
  if(empty($_POST['hectareas']) || !ctype_digit($_POST['hectareas']))
    $errors[] = 'Please enter a valid hectareas number';
  //was a valid number entered for kilos?
  if(empty($_POST['kilos']) || !ctype_digit($_POST['kilos']))
    $errors[] = 'Please enter a valid kilos number';
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

//were there errors in the form?
  echo join('<br />', $errors);
} elseif(isset($_POST)) //form submitted and no errors?
  //do the calculations and display output
  $dosis = ($_POST['hectareas'] * $_POST['kilos']) / 100;
  $bidones = $dosis / 20;
  echo 'Dosis: '.$dosis.'<br />';
  echo 'Bidones: '.$bidones.'<br />';

<form method="post" action="">
  Hectareas <input type="text" name="hectareas" value="" size="5" maxlength="5" /><br />
  Kilos <input type="text" name="kilos" value="" size="5" maxlength="5" /><br />
    <input type="submit" name="submit" value="submit" />


I’m understanding a little more, but not getting there yet.

Earl-grey’s solution give me two input boxes and an OK button, and then the words Dosis and Bidon, but when published to the server, clicking on the OK button returns a 404:
“The requested URL /Productos/Soya/Dosis/< was not found on this server.”

my script is on a page called Productos/Soya/Dosis. what is the “/<” about? Any idea what it can’t find? Is there something else I need to add?

Mark’s solution publishes with errors, showing the following on the screen in text,

', $errors); } elseif(isset($_POST)) //form submitted and no errors? { //do the calculations and display output $Dosis = ($_POST[‘Hectareas’] * $_POST[‘Kilos’]) / 100; $Bidones = $Dosis / 20; echo 'Dosis: ‘.$Dosis.’
'; echo 'Bidones: ‘.$Bidones.’
'; } ?>

I’ve added the phpinfo.php file to my directory that my host says to add. (they supplied).

I’ve just tried both scripts on localhost. To get Mark’s script to work on line 32 I’ve changed isset($_POST) to !empty($_POST). In my script I’ve changed all occurences of $_POST to $_GET, and it worked fine.
It seems that there is some problem with your host, as it looks like it doesn’t execute PHP code. What extension have you used for the script file? You have to use .php extension in order to get script executed.

Making those changes, I can get yours to run on the server as a simple white page, returning the answer on another. But haven’t figured out how to get it into the web page yet.

Mark’s no longer shows the error, but doesn’t do anything.

Got to go BBQ some chicken. Will be working on it later.