#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6

    Unexpected variable


    I'm getting an unexpected variable error on line 14 of this code. I don't see the problem:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    02    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    03    <html xmlns="http://www.w3.org/1999/xhtml">
    04    <head>
    05    <title>Paycheck Results</title>
    06    <meta http-equiv="Content-Type"
    07        content="text/html; charset=iso-8859-1" />
    08    <link rel="stylesheet" href="php_styles.css" type="text/css" />
    09    </head>
    10    <body>
    11    <h1>Paycheck Calculations</h1>
    12    <?php
    14    $hoursWorked 
    $_GET["hours"];
    15    $wages $_GET["wage"];
    16     
    17     
    18    
    echo "You entered that you worked: $hoursWorked hours.<br>";
    19    echo "You entered that your hourly wage is: $wages per hour.<br>";
    20     
    21    
    if ($hoursWorked <= 40)
    22    {
    23        $payCheck = ($hoursWorked $wages);
    24            echo "Your Paycheck is: $payCheck";
    25    }
    26     
    27    
    if ($hoursWorked 40)
    28    {
    29        $payCheck = (($hoursWorked $wages) + (($hoursWorked 40) * $wages 1.5);
    30           echo "Your Paycheck is: $payCheck";
    31    }
    32     
    33     
    34    ?>
    35    </body>
    36    </html>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    What does the URL look like that loads this script? To make sure $_GET contains what you expect put this first:
    PHP Code:
    echo "<pre>";
    print_r($_GET);
    echo 
    "</pre>"
    From a programming practice standpoint you should be checking to make sure the variable you want exists before you try to use it ('isset'). Also you should consider using post rather than get method for your form. This data looks like it could be sensitive and showing it in the URL may not be a good idea. It also lends itself to abuse since it is easy to modify a URL.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    What I am really trying to do is accept the input from the user. Hours and hourly wage. Is there a better way? I still dont understand the variable error.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    The variable error is explicit. That $_GET value you are trying to use does not exist. That is why I asked for the URL. That must contain the variables you are requesting. Presumably you are getting input from the user via a form. Rather then use type=get, use type=post. Access the variables the same way except use $_POST rather than $_GET. You can read up on it here.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    Ok, i have almost got it working i think. I am getting an unexpected ; on line 80 in the code below. I know it should be there. I i take that off, the I get an unexpected } on line 81.

    PHP Code:
    <!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>
    <title>Paycheck Calculator</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>

    <?php
    $errorcount
    =0;
    $wages=$_POST['wages'];
    $hours=$_POST['hours'];


    if (isset(
    $_POST['submit']))
        { 
    //begins if1//
            //echo "<p>The submit button has been pressed.</p>\n";
            
    if (is_numeric($wages))
                {
    //begins if #2
                    
    if ($wages >0)
                    {
    //begins if #3
                        
    $weekly_wages $wages;
                    }
    //ends if #3
                    
    else
                    {
    //goes with if #3
                    
    ++$errorcount;
                     echo 
    "<p>Weekly wage must to be greater than 0</p>";
                    }
    //ends else #3

                
    }//ends if #2
                
    else
                {
    //goes with if #2
                
    ++$errorcount;
                echo 
    "<p>You must enter a number for the wage</p>\n";
                
    //echo "<p>Error Count: " . $errorcount . "</p>\n";
                
    }//ends else #2

    //closes if#1 wage isset




    if (isset($_POST['submit']))
        { 
    //begins if1//
            //echo "<p>The submit button has been pressed.</p>\n";
        
    if (is_numeric($hours))
        {
    //begins if #1 hours

            
    if($wages >= 0)
            {
    //begins if #2 
                
    if ($hours 40)
                {
    //begins if #3 
                
    $bonus true;
                echo 
    "<p>Overtime: " $bonus "</p>\n";
                }
    //ends if #3 

            
    }//ends if #2 
            
    else
            {
    //begins else #2 
            
    ++$errorcount;
            echo 
    "<p>The number of hours must be 0 or higher</p>\n";
            }
    //ends if #2 
        
    }//ends if #1 
        
    else
        {
    //begins else #1 
        
    ++$errorcount;
        echo 
    "<p>The value for hours must be a number</p>\n";
        }
    //ends else #1 
    //closes if#1  isset submit



     
    if ($errorcount == && isset($_POST['submit']))
     {
    //begins if calculations

        
    if ($bonus == 1)
        {
            ((
    $hours $wages) + (($hours 40) * $wages 1.5);
        }
        else
        {
            
    $bonus_amt 0;
        }

        
    $weekly_pay $weekly_wages $bonus_amt;

      echo 
    "<p>Weekly Salary= $" number_format($weekly_salary2) . ".</p>\n";
      echo 
    "<p>Overtime Pay = $" number_format($bonus_amt2) . ".</p>\n";
      echo 
    "<p>Your total Weekly Pay is $" number_format($weekly_pay $bonus_amt2) . ".</p>\n";
      echo 
    "<p><a href = 'PaycheckCalc.html'>Calculate another paycheck?</a></p>\n";

       }
    //ends if 1//
      
    else
      {
    //begins else for calculation
      
    ?>
      <h2 style="text-align:center">Paycheck Calculation</h2>
      <form action=
          "PaycheckCalc.php" method="post">
      <p>Weekly Wage:  <input type="text" name="wages" /></p>

      <p>Number of Hours Worked: <input type="text" name="hours" /></p>
      <p><input type="submit" name="submit" value="Submit"></p>
      </form>

    <?php
    }//ends else to display form

    ?>


    </body>
    </html>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    Without knowing which is line 80 it is tough to say what is wrong. You still need to check your variables before using them or it will crash your script like before.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    this is line 80:

    PHP Code:
    (($hours $wages) + (($hours 40) * $wages 1.5); 
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    i fixed that with a right ). now i get undefined index:

    PHP Code:
    $wages=$_POST["wages"];
    $hours=$_POST['hours']; 
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    6
    still encountering the undefined index on these lines:

    PHP Code:
    $wages $_POST['wages'];
    $hours $_POST['hours']; 
    of this code:
    PHP Code:
    <!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>
    <title>Paycheck Calculator</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>

    <?php
    $errorcount
    =0;
    $wages $_POST['wages'];
    $hours $_POST['hours'];


    if (!isset(
    $_POST['submit']))
        { 
    //begins if1//
            //echo "<p>The submit button has been pressed.</p>\n";
            
    if (is_numeric($wages))
                {
    //begins if #2
                    
    if ($wages >0)
                    {
    //begins if #3
                        
    $weekly_wages $wages;
                    }
    //ends if #3
                    
    else
                    {
    //goes with if #3
                    
    ++$errorcount;
                     echo 
    "<p>Weekly wage must to be greater than 0</p>";
                    }
    //ends else #3

                
    }//ends if #2
                
    else
                {
    //goes with if #2
                
    ++$errorcount;
                echo 
    "<p>You must enter a number for the wage</p>\n";
                
    //echo "<p>Error Count: " . $errorcount . "</p>\n";
                
    }//ends else #2

    //closes if#1 wage isset




    if (!isset($_POST['submit']))
        { 
    //begins if1//
            //echo "<p>The submit button has been pressed.</p>\n";
        
    if (is_numeric($hours))
        {
    //begins if #1 hours

            
    if($wages >= 0)
            {
    //begins if #2 
                
    if ($hours 40)
                {
    //begins if #3 
                
    $bonus true;
                echo 
    "<p>Overtime: " $bonus "</p>\n";
                }
    //ends if #3 

            
    }//ends if #2 
            
    else
            {
    //begins else #2 
            
    ++$errorcount;
            echo 
    "<p>The number of hours must be 0 or higher</p>\n";
            }
    //ends if #2 
        
    }//ends if #1 
        
    else
        {
    //begins else #1 
        
    ++$errorcount;
        echo 
    "<p>The value for hours must be a number</p>\n";
        }
    //ends else #1 
    //closes if#1  isset submit


    $ot_amt 0;
     if (
    $errorcount == && isset($_POST['submit']))
     {
    //begins if calculations

        
    if ($hours 40)
        {
            
    $ot_amt =  (($hours 40) * ($wages 1.5));
            
    $weekly_pay = ($hours $wages) ;
        }
        else
        {
            
    $weekly_pay = ($hours $wages) ;
        }
        
        
    //$weekly_pay = $weekly_wages + $bonus_amt;

      
    echo "<p>Weekly Salary= $" number_format($weekly_pay2) . ".</p>\n";
      echo 
    "<p>Overtime Pay = $" number_format($ot_amt2) . ".</p>\n";
      echo 
    "<p>Your total Weekly Pay is $" number_format($weekly_pay $ot_amt2) . ".</p>\n";
      echo 
    "<p><a href = 'PaycheckCalc.html'>Calculate another paycheck?</a></p>\n";

       }
    //ends if 1//
      
    else
      {
    //begins else for calculation
      
    ?>
      <h2 style="text-align:center">Paycheck Calculation</h2>
      <form action=
          "PaycheckCalc.php" method="post">
      <p>Weekly Wage:  <input type="text" name="wages" /></p>

      <p>Number of Hours Worked: <input type="text" name="hours" /></p>
      <p><input type="submit" name="submit" value="Submit"></p>
      </form>

    <?php
    }//ends else to display form

    ?>


    </body>
    </html>
    I also get this on another script i have. What causes this? Thanks
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    I've already explained it. See my previous responses.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    if (!isset($_POST['submit']))
    only returns true when $_POST['submit'] is not set... you know that right?
  22. #12
  23. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,112
    Rep Power
    487
    Originally Posted by seandisanti
    only returns true when $_POST['submit'] is not set... you know that right?
    Well spotted ...

    PHP Code:
    if (!isset($_POST['submit']))
        { 
    //begins if1//
            //echo "<p>The submit button has been pressed.</p>\n"; 
    reads: if $_POST['submit'] is not set then echo "The submit button has been pressed".

    Remove the ! before the isset to make it work the right way around; as for the undefined variable, try moving the two lines causing this error to after the if statement above, like so:

    PHP Code:
    if (isset($_POST['submit'])) {
     
    $wages $_POST['wages'];
     
    $hours $_POST['hours']; 
     echo 
    "<p>The submit button has been pressed.</p>\n";
    ... 
    Good luck!
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984

IMN logo majestic logo threadwatch logo seochat tools logo