calculate 함수를 호출하여 같은 페이지에 표시하려는 양식이 있습니다. 오류가 있으면 동일한 페이지의 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 I 양식 자체에 텍스트 상자에서 계산 된 결과를 보여주고 싶은 :
<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과 오류 부분을 복사했다. 그러나 그것은 나에게'undefined' 오류를주고있다. – input
와우, 편집을 참조하십시오. if 문 바로 위의'$ error = false; '행을 이동하십시오. –