Ruud Hermans Posted August 7, 2009 Share Posted August 7, 2009 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. 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> Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/ Share on other sites More sharing options...
Mark Baker Posted August 7, 2009 Share Posted August 7, 2009 $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'])) Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892753 Share on other sites More sharing options...
waterssaz Posted August 7, 2009 Share Posted August 7, 2009 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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892754 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 $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. if ($scale == "celcius") to this? if (isset($_GET['celcius'])) I doubt that I understood you the right way. Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892757 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892758 Share on other sites More sharing options...
waterssaz Posted August 7, 2009 Share Posted August 7, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892759 Share on other sites More sharing options...
Mark Baker Posted August 7, 2009 Share Posted August 7, 2009 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. No you don't. You need to add it before you start referencing $scale in your code Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892760 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 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 } Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892761 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 Thanks waterssars and all of you. I understand what you mean. Need to make it a if statement before executing the code. Problem now is when submitting the form it results in a 404 error for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892771 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 What is the form action set to? <form action="what_page_does_this_go_to" method="get"> Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892774 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 What is the form action set to? <form action="what_page_does_this_go_to" method="get"> <form action = "<?php echo $_SERVER[’PHP_SELF’]; ?>" Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892775 Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 Wrong quotes you had ` instead of ' or ". Also added htmlentities to prevent a xss attack <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="get"> Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892779 Share on other sites More sharing options...
waterssaz Posted August 7, 2009 Share Posted August 7, 2009 <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>"> replace yours with above, think characters are wrong around the variable Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892780 Share on other sites More sharing options...
Bjom Posted August 7, 2009 Share Posted August 7, 2009 working with $_SERVER['SCRIPT_NAME'] would be preferrable, since there are security issues with $_SERVER['PHP_SELF'] Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892787 Share on other sites More sharing options...
Ruud Hermans Posted August 7, 2009 Author Share Posted August 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892810 Share on other sites More sharing options...
waterssaz Posted August 7, 2009 Share Posted August 7, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/169201-variable-is-undefined/#findComment-892823 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.