Jump to content

Variable is undefined


Ruud Hermans

Recommended Posts

I have a script here that I am trying to run but for some reason I am recieving errors.it tells e a variable is undefined. While to me it looks like everything is ok. :shrug:

Any suggestions?

Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 22

 

Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 31

 

Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 40

 

Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 50

<html>
<head>
<title></title>
<link href="../style/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table class="pad" width="80%" height="*" border="0" bgcolor ="#FFFFFF" align="center">
<tr>
<td>
<?php include('../includes/secondheader.php') ?>
<h2>Temperature Conversion</h2>
<form action = "<?php echo $_SERVER[’PHP_SELF’]; ?>"
method = "GET">
Degrees:
<input type = "text" name = "degree" size=4>
<select name="scale"> <option value="celcius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> <option value="kelvin">Kelvin</option> <option value="rankine">Rankine</option> </select>
<br/>
<input type = "submit" name = "tets"/>
</form> 

<?php
if ($scale == "celcius")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>celsius</td></tr>";
$c_2_f = $degree*9/5+32;
print "<tr><td>$c_2_f</td><td>fahrenheit</td></tr>";
$c_2_k = $degree+273.15;
print "<tr><td>$c_2_k </td><td>kelvin</td></tr>";
$c_2_r = $c_2_f+459.6;
print "<tr><td>$c_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "fahrenheit")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>farhenheit</td></tr>";
$f_2_c = ($degree -32)*5/9;
print "<tr><td>$f_2_c</td><td>celsius</td></tr>";
$f_2_k = $f_2_c+273.15;
print "<tr><td>$f_2_k </td><td>kelvin</td></tr>";
$f_2_r = $degree+459.6;
print "<tr><td>$f_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "kelvin")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>
$degree</td><td>kelvin</td></tr>";
$k_2_f = ($degree - 273.15) * 9 / 5 + 32;
print "<tr><td>$k_2_f</td><td>fahrenheit</td></tr>";
$k_2_c = $degree-273.15;
print "<tr><td>$k_2_c </td><td>celsius</td></tr>";
$k_2_r = $k_2_f+459.6;
print "<tr><td>$k_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "rankine")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>rankine</td></tr>";
$r_2_f = $degree-459.6;
print "<tr><td>$r_2_f</td><td>fahrenheit</td></tr>";
$r_2_c = ($r_2_f - 32)*5/9;
print "<tr><td>$r_2_c </td><td>celsius</td></tr>";
$r_2_k = $r_2_c + 273.15;
print "<tr><td>$r_2_k</td><td>kelvin</td></tr></table>";} 
?>

<?php include('../includes/footer.php') ?>
</td>
</tr>
</table>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/169201-variable-is-undefined/
Share on other sites

$scale = $_GET['scale']

But it's only set after you've submitted the form, so wrap all the logic that is intended to be run only after submission in an if test

if (isset($_GET['scale']))

I do not get the point. I do not have the queted line of code in the script. Did you mean I need to change. :shrug:

if ($scale == "celcius")

to this?

if (isset($_GET['celcius']))

I doubt that I understood you the right way.

Try this :-)

<html>
<head>
<title></title>
<link href="../style/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table class="pad" width="80%" height="*" border="0" bgcolor ="#FFFFFF" align="center">
<tr>
<td>
<?php include('../includes/secondheader.php') ?>
<h2>Temperature Conversion</h2>
<form action = "<?php echo $_SERVER[’PHP_SELF’]; ?>"
method = "GET">
Degrees:
<input type = "text" name = "degree" size=4>
<select name="scale"> <option value="celcius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> <option value="kelvin">Kelvin</option> <option value="rankine">Rankine</option> </select>
<br/>
<input type = "submit" name = "submit"/>
</form> 

<?php
if((isset($_GET['submit'])))
{
if ($scale == "celcius")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>celsius</td></tr>";
$c_2_f = $degree*9/5+32;
print "<tr><td>$c_2_f</td><td>fahrenheit</td></tr>";
$c_2_k = $degree+273.15;
print "<tr><td>$c_2_k </td><td>kelvin</td></tr>";
$c_2_r = $c_2_f+459.6;
print "<tr><td>$c_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "fahrenheit")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>farhenheit</td></tr>";
$f_2_c = ($degree -32)*5/9;
print "<tr><td>$f_2_c</td><td>celsius</td></tr>";
$f_2_k = $f_2_c+273.15;
print "<tr><td>$f_2_k </td><td>kelvin</td></tr>";
$f_2_r = $degree+459.6;
print "<tr><td>$f_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "kelvin")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>
$degree</td><td>kelvin</td></tr>";
$k_2_f = ($degree - 273.15) * 9 / 5 + 32;
print "<tr><td>$k_2_f</td><td>fahrenheit</td></tr>";
$k_2_c = $degree-273.15;
print "<tr><td>$k_2_c </td><td>celsius</td></tr>";
$k_2_r = $k_2_f+459.6;
print "<tr><td>$k_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "rankine")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>rankine</td></tr>";
$r_2_f = $degree-459.6;
print "<tr><td>$r_2_f</td><td>fahrenheit</td></tr>";
$r_2_c = ($r_2_f - 32)*5/9;
print "<tr><td>$r_2_c </td><td>celsius</td></tr>";
$r_2_k = $r_2_c + 273.15;
print "<tr><td>$r_2_k</td><td>kelvin</td></tr></table>";}

} 
?>

<?php include('../includes/footer.php') ?>
</td>
</tr>
</table>
</body>
</html>











Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 22

Let me explain why this error occurs.

In the following code you check the variable $scale when it doesn't exist yet.

<?php
if ($scale == "celcius") 
?>

 

when you first view the page $scale is not set yet because the form has not been submitted, Add a check submit to solve the problem :-)

What do you mean by check submit?

You are using a form. You need to check if a form was submitted before you can use the values that were send by the form. This is because there are no form values to begin with.

You can check if a form was send like so:

<?php
// using the post method
if(isset($_POST['submit'])){
  // your form submit code here
}

// using the get method
if(isset($_GET['submit'])){
  // your form submit code here
}

 

Going goo goo here.

Full code now:

<link href="../style/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table class="pad" width="80%" height="*" border="0" bgcolor ="#FFFFFF" align="center">
<tr>
<td>
<?php include('../includes/secondheader.php') ?>
<h2>Temperature Conversion</h2>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="get">
Degrees:
<input type = "text" name = "degree" size=4>
<select name="scale"> <option value="celcius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> <option value="kelvin">Kelvin</option> <option value="rankine">Rankine</option> </select>
<br/>
<input type = "submit" name = "submit"/>
</form>

<?php
if((isset($_GET['submit'])))
{
if ($scale == "celcius")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>celsius</td></tr>";
$c_2_f = $degree*9/5+32;
print "<tr><td>$c_2_f</td><td>fahrenheit</td></tr>";
$c_2_k = $degree+273.15;
print "<tr><td>$c_2_k </td><td>kelvin</td></tr>";
$c_2_r = $c_2_f+459.6;
print "<tr><td>$c_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "fahrenheit")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>farhenheit</td></tr>";
$f_2_c = ($degree -32)*5/9;
print "<tr><td>$f_2_c</td><td>celsius</td></tr>";
$f_2_k = $f_2_c+273.15;
print "<tr><td>$f_2_k </td><td>kelvin</td></tr>";
$f_2_r = $degree+459.6;
print "<tr><td>$f_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "kelvin")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>
$degree</td><td>kelvin</td></tr>";
$k_2_f = ($degree - 273.15) * 9 / 5 + 32;
print "<tr><td>$k_2_f</td><td>fahrenheit</td></tr>";
$k_2_c = $degree-273.15;
print "<tr><td>$k_2_c </td><td>celsius</td></tr>";
$k_2_r = $k_2_f+459.6;
print "<tr><td>$k_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "rankine")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>rankine</td></tr>";
$r_2_f = $degree-459.6;
print "<tr><td>$r_2_f</td><td>fahrenheit</td></tr>";
$r_2_c = ($r_2_f - 32)*5/9;
print "<tr><td>$r_2_c </td><td>celsius</td></tr>";
$r_2_k = $r_2_c + 273.15;
print "<tr><td>$r_2_k</td><td>kelvin</td></tr></table>";}

} 
?>

<?php include('../includes/footer.php') ?>
</td>
</tr>
</table>
</body>
</html>

And now I am recieving this again.

Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 20

 

Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 29

 

Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 38

 

Notice: Undefined variable: scale in D:\Test Server\EasyPHP 3.0\EasyPHP3.1\www\SPT\tools\temp.php on line 48

:shrug:

This will work. points to note:

1) You do not set $scale or $degree in your script( I have changed this);

2) You do not give your option values names so that they cab be referenced.

 

<html>
<head>
<link href="../style/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table class="pad" width="80%" height="*" border="0" bgcolor ="#FFFFFF" align="center">
<tr>
<td>
<?php include('../includes/secondheader.php') ?>
<h2>Temperature Conversion</h2>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="get">
Degrees:
<input type = "text" name = "degree" size=4>
<select name="scale"> <option value="celcius" name="celcius">Celsius</option> <option value="fahrenheit" name="fahrenheit">Fahrenheit</option> <option value="kelvin" name="kelvin">Kelvin</option> <option value="rankine" name = "rankine">Rankine</option> </select>
<br/>
<input type = "submit" name = "submit"/>
</form>

<?php
if((isset($_GET['submit'])))
{
$scale= $_GET['scale'];
$degree = $_GET['degree'];
if ($scale == "celcius")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>celsius</td></tr>";
$c_2_f = $degree*9/5+32;
print "<tr><td>$c_2_f</td><td>fahrenheit</td></tr>";
$c_2_k = $degree+273.15;
print "<tr><td>$c_2_k </td><td>kelvin</td></tr>";
$c_2_r = $c_2_f+459.6;
print "<tr><td>$c_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "fahrenheit")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>farhenheit</td></tr>";
$f_2_c = ($degree -32)*5/9;
print "<tr><td>$f_2_c</td><td>celsius</td></tr>";
$f_2_k = $f_2_c+273.15;
print "<tr><td>$f_2_k </td><td>kelvin</td></tr>";
$f_2_r = $degree+459.6;
print "<tr><td>$f_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "kelvin")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>
$degree</td><td>kelvin</td></tr>";
$k_2_f = ($degree - 273.15) * 9 / 5 + 32;
print "<tr><td>$k_2_f</td><td>fahrenheit</td></tr>";
$k_2_c = $degree-273.15;
print "<tr><td>$k_2_c </td><td>celsius</td></tr>";
$k_2_r = $k_2_f+459.6;
print "<tr><td>$k_2_r</td><td>rankine</td></tr></table>";}

if ($scale == "rankine")
{print "<table border><tr><th colspan=2> Conversion Results</th></tr><tr><td>$degree</td><td>rankine</td></tr>";
$r_2_f = $degree-459.6;
print "<tr><td>$r_2_f</td><td>fahrenheit</td></tr>";
$r_2_c = ($r_2_f - 32)*5/9;
print "<tr><td>$r_2_c </td><td>celsius</td></tr>";
$r_2_k = $r_2_c + 273.15;
print "<tr><td>$r_2_k</td><td>kelvin</td></tr></table>";}

} 
?>

<?php include('../includes/footer.php') ?>
</td>
</tr>
</table>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.