jimjimalabim Posted February 17, 2011 Share Posted February 17, 2011 Hello people I'm having some trouble, and I kind think I know what it is, but for some reason I just can't figure it out. I think I need an isset function, but I'm not sure where to put it? Here is my code followed by my errors. <?php echo"<?xml version=\1.0\"encoding=\UFT-8\"\x3f>"; echo"\n"; ?> <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"lang="en"> <head> <title>My Second PHP Generated Document</title> </head> <body> <?php if (isset($_POST['submit'])) { if ($_POST['submit'] == "Clear") { $workValue = ""; $workType = ""; } elseif ($_POST['submit'] == "Calculate") { $workValue = $_POST['vfld']; $workType = $_POST['tfld']; $calcValue = (float) $workValue; if (empty($_POST['vfld'])) { echo "<h3>Error</h3>"; echo "<p>You must enter a value!</p>\n"; } elseif ($calcValue == 0) { echo "<h3>Error</h3>"; echo "<p>You must enter a number!</p>\n"; } elseif ($calcValue < 0) { echo "<h3>Error</h3>"; echo "<p>You must enter a positive number!</p>\n"; } elseif (empty($_POST['tfld'])) { echo "<h3>Error</h3>"; echo "<p>You must select a calculation type!</p>\n"; }} else { switch ($workType) { case "circle": $areaValue = $calcValue * $calcValue * pi(); break; case "square": $areaValue = $calcValue * $calcValue; // echo "The area of a square whose sides are" break; case "triangle": $halfSide = $calcValue/2; $otherSide = sqrt(($calcValue * calcValue) - ($halfSide * halfSide)); $areaValue = $otherSide * $halfSide; echo "The area of an equilateral triangle whose sides are $calcValue} is {areaValue}."; break; }} } ?> <form method="post" action="hw04.php" id="form1" name="form1"> <p> Enter a number here : <input type="text" id="vfld" name="vfld" value="<?php echo $workValue; ?>"/> </p> <p> This number is a: <br /> <input type="radio" name="tfld" value="circle" <?php if ($workType == "circle") { echo 'checked="checked"'; } ?> /> : the radius of a circle <br /> <input type="radio" name="tfld" value="square" <?php if ($workType == "square") { echo 'checked="checked"'; } ?> /> : the length of one side of a square <br /> <input type="radio" name="tfld" value="triangle" <?php if ($workType == "triangle") { echo 'checked="checked"'; } ?> /> : the length of one side of an equilateral triangle </p> <p> <input type="submit" name="submit" value="Calculate" /> <input type="submit" name="submit" value="Clear" /> </p> </form> <div id="footer"> <pre>This page written by: <cite>James Cozzy</cite> ©2011 and beyond</pre> </div> </body> </html> And my errors Enter a number here : This number is a: Notice: Undefined variable: workType in /studfs1/j-cozzy/homepage/CISS-225/hw04.php on line 69 /> : the radius of a circle Notice: Undefined variable: workType in /studfs1/j-cozzy/homepage/CISS-225/hw04.php on line 74 /> : the length of one side of a square Notice: Undefined variable: workType in /studfs1/j-cozzy/homepage/CISS-225/hw04.php on line 79 /> : the length of one side of an equilateral triangle any help would be much appreciated? Link to comment https://forums.phpfreaks.com/topic/227953-beginer-having-trouble/ Share on other sites More sharing options...
kenrbnsn Posted February 17, 2011 Share Posted February 17, 2011 Those undefined variables are only defined in your script after the form is submitted. The first time through, they aren't defined. You should initialize them at the start of your script by doing something like <?php $workValue = (isset($_POST['vfld']))?$_POST['vfld']:''; $workType = (isset($_POST['tfld']))?$_POST['tfld']:''; ?> Note: when you post code to this forum, please put it withing tags. Ken Link to comment https://forums.phpfreaks.com/topic/227953-beginer-having-trouble/#findComment-1175488 Share on other sites More sharing options...
jimjimalabim Posted February 17, 2011 Author Share Posted February 17, 2011 Thank you, that helped. I'm still having some issues, but I will work through them, i'm sure if it gets too confusing I will be back. What do you mean post code within [ code] ? Again thank you, it looks like you turned them into global variables? Or maybe you can explain a little more what you did? Link to comment https://forums.phpfreaks.com/topic/227953-beginer-having-trouble/#findComment-1175820 Share on other sites More sharing options...
MatthewJ Posted February 17, 2011 Share Posted February 17, 2011 Ken's statements just mean "If the variable is set, set the value of what is in front of the equals to that value, if not set it to blank". That way, your variables in front of the equals always have a value. Hence they are no longer "Undefined" Link to comment https://forums.phpfreaks.com/topic/227953-beginer-having-trouble/#findComment-1175859 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.