internetallstar Posted August 23, 2012 Share Posted August 23, 2012 Having trouble finding the right solution for errors in php. The error reads..... Notice: Undefined variable: selected in C:\wamp\www\tcpro_34000\register.php on line 264 Call Stack # Time Memory Function Location 1 0.0124 200688 {main}( ) ..\register.php:0 Line 264 then reads... echo "<option class=\"option\" value=\"".$row['groupname']."\"".$selected.">".$row['groupname']."</option>\n\r"; $selected=""; Any idea why this is happening? Any help would be nice thanks. Quote Link to comment https://forums.phpfreaks.com/topic/267476-help-with-undefined-variable/ Share on other sites More sharing options...
KevinM1 Posted August 23, 2012 Share Posted August 23, 2012 Judging by that error message, $selected has no value (hence, undefined), but you're trying to echo it anyway. Quote Link to comment https://forums.phpfreaks.com/topic/267476-help-with-undefined-variable/#findComment-1371754 Share on other sites More sharing options...
internetallstar Posted August 23, 2012 Author Share Posted August 23, 2012 Here is the rest of the coding dealing with the option menu box... 52 if ( isset($_POST['btn_submit']) AND in_array($_POST['lst_group'],$G->getGroups()) ) 53 { 54 if (!strlen($_POST['txt_lastname']) || !strlen($_POST['txt_username']) || !strlen($_POST['txt_email']) || & <?php $groups = $G->getAll(); foreach ($groups as $row) { if ( isset($_POST['lst_group']) && strlen($_POST['lst_group']) && ($row['groupname']==$_POST['lst_group']) ) $selected=" SELECTED"; echo "<option class=\"option\" value=\"".$row['groupname']."\"".$selected.">".$row['groupname']."</option>\n\r"; $selected=""; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/267476-help-with-undefined-variable/#findComment-1371758 Share on other sites More sharing options...
KevinM1 Posted August 23, 2012 Share Posted August 23, 2012 Line 54 is filled with syntax errors. You have an extra '||' at the end, an unneeded '&' at the end, and no closing parentheses. Also, your $selected variable is not guaranteed to have SELECTED as its value with the way you structured your if-conditional. An if (or else) without braces ( {} ) tells PHP that only the statement immediately following it will be executed if the condition is met. In other words, there's a big difference between: if(/*stuff*/) $selected = " SELECTED"; echo "..."; and if(/*stuff*/) { $selected = " SELECTED"; echo "..."; } In the first example, $selected is set if the condition is met, but the echo will always execute. In the second example, both the $selected assignment and echo are dependent on the if condition being met. Finally, in the future, when posting code on these forums, place it between either or tags. Quote Link to comment https://forums.phpfreaks.com/topic/267476-help-with-undefined-variable/#findComment-1371762 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.