我有一个窗体,我希望计算函数被调用并显示在同一页面上。如果出现错误,它应显示在同一页面上的span标签中。提交并在窗体中显示结果在同一页
这就是我现在所拥有的,而且我遇到了问题:
的index.php [增订]:
<?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>
主要有两件事情我想在展示形式:
1-如果有错误,它应在范围标记显示错误 -
<?php
if (isset($_REQUEST['error'])){
$err = $_REQUEST['error'];
?>
<div id="er"> <span class="er" > <?php echo $err; ?></span></div>
<?php } ?>
我这样做了^,但即使文本框为空,它也不会抛出任何错误。
2 - 我想显示在窗体本身的文本框计算结果:
<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; ?>" />
^这是抛出一个错误:Undefined variable nbnm and twkg
我要去哪里错了?
谢谢你的解释。我试图实现你的代码,但它会抛出这个错误:'未定义的变量:错误'。我不知道为什么。我做了一个新的PHP页面,只是复制了HTML和错误部分,但它给了我'未定义'错误。 – input
哎呦,看编辑。在if语句上移动'$ error = false;'行。 –