Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/267476-help-with-undefined-variable/
Share on other sites

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="";

                    }

                    ?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.