简体   繁体   English

PHP验证并将信息存储到会话变量中

[英]PHP Validation and storing information into Session Variables

I'm setting up 2 text-boxes and one drop down options list in html. 我在html中设置了2个文本框和一个下拉选项列表。 I'm trying to use php to validate if these text box are filled in or not. 我正在尝试使用php来验证这些文本框是否已填写。 If clicked on the submit button ('btnCalculate'), the validation occurs, if nothing in the text boxes, there will be an error message that pops up beside it ('pamountErrorMsg, irateErrorMsg'). 如果单击提交按钮('btnCalculate'),则会进行验证,如果文本框中没有任何内容,则会在其旁边弹出一条错误消息('pamountErrorMsg,irateErrorMsg')。 If there is substance that is typed into the textboxes, the browser will save the information into Session["pamount"] or "irate". 如果在文本框中键入了实体,浏览器会将信息保存到Session [“pamount”]或“irate”中。 I am having trouble validating the blank text boxes and adding the variables into Session before I can use them in the next page. 我无法验证空白文本框并将变量添加到Session中,然后才能在下一页中使用它们。 Right now it is not validating anything at all. 现在它根本没有验证任何东西。 What is wrong with my syntax, if any? 我的语法有什么问题,如果有的话?

Was thinking of using just using "php? $pamount = $_SESSION["pamount"]; ?>" to create the variable I need to read the session variable, is that correct? 想要使用“php?$ pamount = $ _SESSION [”pamount“];?>”创建我需要读取会话变量的变量,这是正确的吗?

PHP 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 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>&nbsp;</td>
            <td>
                <input type='submit' class='button' name='btnCalculate' value='Calculate'/>&nbsp;&nbsp;
                <input type='reset' class='button' name='btnReset' value='Reset' />

            </td>
        </tr>
</table>
    </form>

My Result.php ( in progress ) 我的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>

You need to do the validation on the same page, where the HTML form is given ie Calculator.php. 您需要在同一页面上进行验证,其中给出了HTML表单,即Calculator.php。 On successful validation you will assign the entered values to session variables, and redirect to Result.php where you can do the calculation and display result. 成功验证后,您将输入的值分配给会话变量,并重定向到Result.php,您可以在其中进行计算和显示结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM