PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 7th, 2012, 06:49 PM
maineman maineman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 112 maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
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> 

Reply With Quote
  #2  
Old October 7th, 2012, 07:33 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 1 Day 10 h 52 m 25 sec
Reputation Power: 581
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.

Reply With Quote
  #3  
Old October 7th, 2012, 07:38 PM
maineman maineman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 112 maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
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.

Reply With Quote
  #4  
Old October 7th, 2012, 08:25 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 1 Day 10 h 52 m 25 sec
Reputation Power: 581
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.

Reply With Quote
  #5  
Old October 7th, 2012, 08:26 PM
maineman maineman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 112 maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
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> 

Reply With Quote
  #6  
Old October 7th, 2012, 08:38 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 1 Day 10 h 52 m 25 sec
Reputation Power: 581
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.

Reply With Quote
  #7  
Old October 7th, 2012, 08:42 PM
maineman maineman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 112 maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
this is line 80:

PHP Code:
(($hours $wages) + (($hours 40) * $wages 1.5); 

Reply With Quote
  #8  
Old October 7th, 2012, 08:47 PM
maineman maineman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 112 maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
i fixed that with a right ). now i get undefined index:

PHP Code:
 $wages=$_POST["wages"];
$hours=$_POST['hours']; 

Reply With Quote
  #9  
Old October 7th, 2012, 11:06 PM
maineman maineman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 112 maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level)maineman User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
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

Reply With Quote
  #10  
Old October 8th, 2012, 07:01 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 1 Day 10 h 52 m 25 sec
Reputation Power: 581
I've already explained it. See my previous responses.

Reply With Quote
  #11  
Old October 8th, 2012, 03:37 PM
seandisanti seandisanti is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 seandisanti User rank is Private First Class (20 - 50 Reputation Level)seandisanti User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 58 m 26 sec
Reputation Power: 0
Quote:
if (!isset($_POST['submit']))

only returns true when $_POST['submit'] is not set... you know that right?

Reply With Quote
  #12  
Old October 9th, 2012, 03:23 AM
badger_fruit's Avatar
badger_fruit badger_fruit is offline
Confused badger
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Location: West Yorkshire
Posts: 762 badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 5 h 28 m 2 sec
Reputation Power: 339
Quote:
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!
__________________
The number for UK Emergencies is changing, the new number is 0118 999 881 999 119 7253

"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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Unexpected variable

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap