PHP Form - Keeping Values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beaner_06
Forum Newbie
Posts: 13
Joined: Tue May 19, 2009 6:11 pm

PHP Form - Keeping Values

Post by beaner_06 »

Hello, I have attached my code I am working with. The form is calling itself and after submission, the form should keep the values that the user had entered and then display the result of the commission amount. I'm not sure where I am going wrong, but the form submits and it displays the GET information in the URL, but nothing shows up. Any help is appreciated.

Code: Select all

 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Commission</title>
</head>
<body>
<h1>Commission Computation</h1>
 
    <form method="get" action='rlbSinglePreserve.php'>
    <?php
    //Form should keep values entered by user after submission
    if(isset($_GET["strSale"])) 
    {
        $strSale = $_GET["sale"];
        $lboRegion = $_GET["Region"];
        
        echo "Monthy Sale: <input name='sale' type='text' value='$strSale' /><br/><br/>";
        echo "Select your Sales Region:";
        echo "<select name='Region' size='4' style='height:38px;'>";
        echo "<option name='eastern'>Eastern</option>";
        echo "<option name='western'>Western</option>";
        echo "</select><br/><br/>";
        echo "<input type='submit' value='Show Commission'/>";
 
        //Depending on region will decide amount of commission that person receives
            switch ($lboRegion)
            {
                Case "Eastern":
                    $commission = $strSale * 0.1;
                    break;
                Case "Western":
                    $commission = $strSale * 0.2;
                    break;
            }
            
        echo "Your Commission is: $$commission ";
    }
 
    else 
    {
        echo "Monthy Sale: <input name='sale' type='text'/><br/><br/>";
        echo "Select your Sales Region:";
        echo "<select name='Region' size='4' style='height:38px;'>";
        echo "<option name='eastern'>Eastern</option>";
        echo "<option name='western'>Western</option>";
        echo "</select><br/><br/>";
        echo "<input type='submit' value='Show Commission'/>";
    }
    ?>
    </form>
</body>
</html>
 
cone13cone
Forum Newbie
Posts: 24
Joined: Fri Mar 20, 2009 8:32 pm

Re: PHP Form - Keeping Values

Post by cone13cone »

Your problem was that the $_GET index you were looking for in the if statement, I just added a name ='submit' to the submit button and then look for $_GET['submit'] after the form submits itself.

Code: Select all

 
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Commission</title>
</head>
<body>
<h1>Commission Computation</h1>
 
    <form method="get" action=" <?php echo $_SERVER['PHP_SELF']; ?> >
    <?php
    //Form should keep values entered by user after submission
    if(isset($_GET["submit"]))
    {
        
        $strSale = $_GET["sale"];
        $lboRegion = $_GET["Region"];
       
 
        //Depending on region will decide amount of commission that person receives
            switch ($lboRegion)
            {
                Case "Eastern":
                    $commission = $strSale * 0.1;
                    break;
                Case "Western":
                    $commission = $strSale * 0.2;
                    break;
            }
          
        
        // assuming its in USD format apply a format function
        echo "<h4>Your Commission is: ".number_format(($commission), 2, '.', ',')."</h4>"; 
    }
 
    // Remove the else to allow the form to display again, if you do not want it to display wrap it with the else{} again
    echo "Monthy Sale: <input name='sale' type='text'/><br/><br/>";
    echo "Select your Sales Region:";
    echo "<select name='Region' size='4' style='height:38px;'>";
    echo "<option name='eastern'>Eastern</option>";
    echo "<option name='western'>Western</option>";
    echo "</select><br/><br/>";
    echo "<input name='submit' type='submit' value='Show Commission'/>";
    ?>
</body>
</html>
 
cone13cone
Forum Newbie
Posts: 24
Joined: Fri Mar 20, 2009 8:32 pm

Re: PHP Form - Keeping Values

Post by cone13cone »

if you want to retain values after submission try something like this:

Code: Select all

 
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Commission</title>
</head>
<body>
<h1>Commission Computation</h1>
 
    <form method="get" action=" <?php echo $_SERVER['PHP_SELF']; ?> >
    <?php
    //Form should keep values entered by user after submission
    if(isset($_GET["submit"]))
    {
        //Depending on region will decide amount of commission that person receives
            switch ($_GET["region"])
            {
                Case "Eastern":
                    $commission = $_GET["sale"] * 0.1;
                    break;
                Case "Western":
                    $commission = $_GET["sale"] * 0.2;
                    break;
            }
          
        // assuming its in USD format apply a format function
        echo "<h4>Your Commission is: $".number_format(($commission), 2, '.', ',')."</h4>"; 
    }
 
    //Preset form fields if form has been submited
    echo "Monthy Sale: <input name='sale' type='text'";
    if(isset($_GET["sale"])){
        echo " value = '".$_GET["sale"]."'";
    }
    echo "/><br/><br/>";
    echo "Select your Sales Region:";
    echo "<select name='region' size='4' style='height:38px;'>";
    $regions = array('Eastern', 'Western');
    foreach($regions as $region){
        echo "<option";
        if(isset($_GET["region"])){
            if($_GET["region"] == $region){
                echo " selected";
            }
        }
        echo ">".$region."</option>";
    }
    echo "</select><br/><br/>";
    echo "<input name='submit' type='submit' value='Show Commission'/>";
    ?>
</body>
</html>
 
 
Post Reply