我正在HTML中设置两个文本框和一个下拉选项列表。我尝试使用PHP验证这些文本框是否已填写。如果点击提交按钮('btnCalculate'),则进行验证,如果文本框中没有任何内容,则会弹出错误消息('pamountErrorMsg,irateErrorMsg')。如果在文本框中输入了一些内容,则浏览器将把信息保存到Session ["pamount"]或"irate"中。在将它们用于下一页之前,我无法验证空白文本框并将变量添加到Session中。现在根本不进行任何验证。我的语法有什么问题吗?
我考虑只使用 "php?$pamount = $ _SESSION [“pamount”];?" 来创建所需的变量以读取会话变量,这是正确的吗? PHP
我考虑只使用 "php?$pamount = $ _SESSION [“pamount”];?" 来创建所需的变量以读取会话变量,这是正确的吗? PHP
session_start(); // start PHP session!
header('Cache-Control: no-cache');
header('Pragma: no-cache');
$pamountErrorMsg = "";
$irateErrorMsg = "";
$btnCalculate = $_GET['btnCalculate'];
$irate = $_GET['irate'];
$pamount = $_GET['pamount'];
$y2d = $_GET['y2d'];
if(isset($btnCalculate))
{
if(strlen(trim($pamount)) <= 0)
{
$pamountErrorMsg = "Principal Amount cannot be empty";
$irateErrorMsg = "";
}
else if(strlen(trim($irate)) <= 0)
{
$irateErrorMsg = "The interest rate cannot be empty";
$pamountErrorMsg = "";
}
else
{
$_SESSION["pamount"] = $_GET["pamount"];
$_SESSION["irate"] = $_GET["irate"];
$_SESSION["y2d"] = $_GET["y2d"];
header("Location: Lab3DisclamerZCL.php");
exit( );
}
}
?>
HTML
<form method='get' action="Result.php">
<table>
<tr>
<td>
Principal Amount
</td>
<td>
<input type='text' class='input' name='pamount' size='30' />
</td>
<td class='error'>
<?php echo $pamountErrorMsg; ?>
</td>
</tr>
<tr>
<td>
Interest Rate (%)
</td>
<td>
<input type='text' class='input' name='irate' size='30' />
</td>
<td class='error'>
<?php echo $irateErrorMsg; ?>
</td>
</tr>
<tr>
<td>
Years to Deposite
</td>
<td>
<select name='y2d' selected='5'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5' selected='selected'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type='submit' class='button' name='btnCalculate' value='Calculate'/>
<input type='reset' class='button' name='btnReset' value='Reset' />
</td>
</tr>
</table>
</form>
我的Result.php(正在进行中)
<?php error_reporting(E_ALL); ?>
<?php
session_start(); // retrieve PHP session!
header('Cache-Control: no-cache');
header('Pragma: no-cache');
if (!isset($_SESSION["name"]))
{
header("Location: Calculator.php");
exit( );
}
if(!isset($_SESSION["pamount"]))
{
$pamount = $_SESSION["pamount"];
}
if(!isset($_SESSION["irate"]))
{
$irate = $_SESSION["irate"];
}
if(!isset($_SESSION["pamount"]))
{
$y2d = $_SESSION["y2d"];
}
?>
<html>
<head>
<title>Results</title>
<link rel = "stylesheet" type = "text/css" href = "style.css" />
</head>
<body>
<h3 class="distinct">Thank you <?php echo $_SESSION["name"]?>, for using out deposite calculation tool.</h3>
<p>Following is the results of calculation </p>
<form action='Result.php' method='post'>
<table>
<?php
$pamount = $_SESSION["pamount"];
$irate = $_SESSION["irate"];
$y2d = $_SESSION["y2d"];
$runningPrincipal = $pamount;
for($i = 1; $i <= $y2d; ++$i)
{
$interest = $runningPrincipal * $irate * 0.01;
printf("<tr><td>%s</td><td>\$%.2f</td><td>\$%.2f</td></tr>", $i, $runningPrincipal, $interest);
$runningPrincipal += $interest;
}
?>
</table>
</form>
<?php session_destroy();?>
</body>
</html>
<form method ='get' action ="Result.php">
),因此无法使用$ _POST
设置会话变量。请使用$ _GET
或更好的$ _REQUEST
。 - air4xResult.php
还是Lab3DisclamerZCL.php
? - air4x