I've a form in which I want the calculate function to be called and displayed on the same page. If there is an error, it should display in the span tag on the same page.
Here's what I have right now, and I'm running into problems:
index.php [UPDATED]:
<?php
if (isset($_POST['submit'])) {
$bdmm = $_POST['bdmm'];
$sdmm = $_POST['sdmm'];
$strings = array(
$bdmm,
$sdmm,
$lgpm
);
if(!$bdmm){
$error = "Error";
exit();
}
elseif(!$sdmm){
$error = "Error";
exit();
}
//check whether the string is numeric
foreach ($string as $string) {
if (!preg_match("/^-?([0-9])+\.?([0-9])+$/", $string))
{
echo "Invalid entry. Please enter a number.";
}
}
$calc = new Calc();
if (isset($bdmm)) {
$calc->calculateMet($bdmm,$sdmm);
}
}
// Defining the "calc" class
class Calc {
private $vs = 0;
private $nob = 0;
private $tw = 0;
public function calculateMet($b,$s)
{
//code to calculate
//display nbnm in textbox
$nbnm = $nobr;
//display twkg in textbox
$tw = $nob * 25;
$twr = round($tw);
$twkg = $tw;
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<!-- Begin Wrapper -->
<div id="wrapper">
<div id="column">
<form id="deps" name="deps" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<?php
if (isset($_REQUEST['error'])){
$err = $_REQUEST['error'];
?>
<div id="er"> <span class="er" > <?php echo $err; ?></span></div>
<?php } ?>
<table class="table">
<tr>
<td class="field head"> </td>
<td class="field cv"><label for="met">Metric</label></td>
<td class="field cv"><label for="eng">English</label></td>
</tr>
<tr>
<td class="field head"><label for="bd">Bore Dia.</label></td>
<td class="field"><input type="text" name="bdmm" id="bdmm" /><label for="bdmm">MM</label></td>
</tr>
<tr>
<td class="field head"><label for="sd">Screen Dia.</label></td>
<td class="field"><input type="text" name="sdmm" id="sdmm" /> <label for="sdmm">MM</label></td>
</tr>
<tr>
<td class="field head"><label for="nbn">No. of Bags needed</label></td>
<td class="field"><input type="text" name="nbnm" id="nbnm" value="<?php echo $nbnm; ?>" /></td>
</tr>
<tr>
<td class="field head"><label for="tw">Total Weight</label></td>
<td class="field"><input type="text" name="twkg" id="twkg" value="<?php echo $twkg; ?>" /> <label for="twkg">KG</label></td>
</tr>
</table>
<input type="submit" id="submit" value="Calculate" />
</form>
</div>
</div>
<!-- End Wrapper -->
</body>
</html>
There are mainly two things I want to show in the form:
1- If there is an error, it should display the error in the span tag -
<?php
if (isset($_REQUEST['error'])){
$err = $_REQUEST['error'];
?>
<div id="er"> <span class="er" > <?php echo $err; ?></span></div>
<?php } ?>
I did this^, but it does not throw any error even if the textbox is blank.
2- I want to show the calculated results in the textboxes in the form itself:
<td class="field"><input type="text" name="nbnm" id="nbnm" value="<?php echo $nbnm; ?>" /></td>
<td class="field"><input type="text" name="twkg" id="twkg" value="<?php echo $twkg; ?>" />
^This is throwing an error: Undefined variable nbnm and twkg
Where am I going wrong?