[英]PHP Validation and storing information into Session Variables
我在html中設置了2個文本框和一個下拉選項列表。 我正在嘗試使用php來驗證這些文本框是否已填寫。 如果單擊提交按鈕('btnCalculate'),則會進行驗證,如果文本框中沒有任何內容,則會在其旁邊彈出一條錯誤消息('pamountErrorMsg,irateErrorMsg')。 如果在文本框中鍵入了實體,瀏覽器會將信息保存到Session [“pamount”]或“irate”中。 我無法驗證空白文本框並將變量添加到Session中,然后才能在下一頁中使用它們。 現在它根本沒有驗證任何東西。 我的語法有什么問題,如果有的話?
想要使用“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>
您需要在同一頁面上進行驗證,其中給出了HTML表單,即Calculator.php。 成功驗證后,您將輸入的值分配給會話變量,並重定向到Result.php,您可以在其中進行計算和顯示結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.