JeanieTallis Posted April 2, 2009 Share Posted April 2, 2009 I recieve this error when using the code below; Parse error: syntax error, unexpected T_ELSE in /home/myveeco/public_html/include/session.php on line 315 <?php /* Email error checking */ $field = "email"; //Use field name for email if(!$subemail || strlen($subemail = trim($subemail)) == 0){ $form->setError($field, "* Email not entered"); } else{ /* Check if valid email address */ $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if(!eregi($regex,$subemail)){ $form->setError($field, "* Email invalid"); } $subemail = stripslashes($subemail); } else{ if($database->emailTaken($subemail)){ $form->setError($field, "* Email already in use"); } } ?> Line 315 is this "else{" Just above "if($database->emailTaken($subemail)){" I want it so it checks if an email is entered, then checks if the email is valid, then checks if the email is already in use. If it is in use, it gives en error as shown there, the problem isn't the code itself, its the if and else statements. Don't know how to work it out. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/ Share on other sites More sharing options...
jonsjava Posted April 2, 2009 Share Posted April 2, 2009 you can only use else AFTER you have used an "if" or "elseif" statement. You cannot use an "else" after an "else" statement. That's like saying "Johnny will go the park if it's not raining, or else he will stay home, or else he will go to the mall". You cannot have an else else. It makes no sense. Here's an idea (not sure if it will work...) <?php /* Email error checking */ $field = "email"; //Use field name for email if(!$subemail || strlen($subemail = trim($subemail)) == 0){ $form->setError($field, "* Email not entered"); } else{ /* Check if valid email address */ $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if(!eregi($regex,$subemail)){ $form->setError($field, "* Email invalid"); } $subemail = stripslashes($subemail); } if($database->emailTaken($subemail)){ $form->setError($field, "* Email already in use"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799851 Share on other sites More sharing options...
JeanieTallis Posted April 2, 2009 Author Share Posted April 2, 2009 This error now occurs Parse error: syntax error, unexpected '{' in /home/myveeco/public_html/include/database.php on line 131 Line 131 = if(!$subuser || strlen($subuser = trim($subuser)) == 0){ The block of code is <?php function login($subuser, $subpass, $subremember){ global $database, $form; //The database and form object /* Username error checking */ $field = "user"; //Use field name for username if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered"); } else{ /* Check if username is not alphanumeric */ if(!eregi("^([0-9a-z])*$", $subuser)){ $form->setError($field, "* Username not alphanumeric"); } } ?> Thanks. (none of these errors use to occur until I tried to put in 'email is being used' part. Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799857 Share on other sites More sharing options...
jonsjava Posted April 2, 2009 Share Posted April 2, 2009 knowing now what you were wanting, I think I fixed it for you: <?php function login($subuser, $subpass, $subremember){ global $database, $form; //The database and form object /* Username error checking */ $field = "user"; //Use field name for username if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered"); } /* Check if username is not alphanumeric */ if(!eregi("^([0-9a-z])*$", $subuser)){ $form->setError($field, "* Username not alphanumeric"); } if($database->emailTaken($subemail)){ $form->setError($field, "* Email already in use"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799860 Share on other sites More sharing options...
JeanieTallis Posted April 2, 2009 Author Share Posted April 2, 2009 Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/myveeco/public_html/include/session.php on line 143 <?php $field = "pass"; //Use field name for password if(!$subpass){ $form->setError($field, "* Password not entered"); } /* Return if form errors exist */ if($form->num_errors > 0){ return false; }?> Line 143 = $field = "pass"; //Use field name for password Shall I put all the origional code, before I started the email is in use bit, so when it last worked properly, so it'll help figure out where 'email is not in use' should go? Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799862 Share on other sites More sharing options...
jonsjava Posted April 2, 2009 Share Posted April 2, 2009 PM me the code. I'll look at it when I get home (on my way out the door now) Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799863 Share on other sites More sharing options...
redarrow Posted April 2, 2009 Share Posted April 2, 2009 <?php /* Email error checking */ $field = "email"; //Use field name for email if(!$subemail || strlen($subemail = trim($subemail)) == 0){ $form->setError($field, "* Email not entered"); } $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if(!eregi($regex,$subemail)){ $form->setError($field, "* Email invalid"); } $subemail = stripslashes($subemail); if($database->emailTaken($subemail)){ $form->setError($field, "* Email already in use"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799866 Share on other sites More sharing options...
JeanieTallis Posted April 2, 2009 Author Share Posted April 2, 2009 Good job, redarrow. No errors with that one I still have location to put in. Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799919 Share on other sites More sharing options...
redarrow Posted April 2, 2009 Share Posted April 2, 2009 i fixed it, add location any where before the brace ends. Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799923 Share on other sites More sharing options...
JeanieTallis Posted April 2, 2009 Author Share Posted April 2, 2009 I'm not too sure what it would be. I have a guess. This is the whole register form code in session.php function register($subuser, $subpass, $subemail, $sublocation){ global $database, $form, $mailer; //The database, form and mailer object /* Username error checking */ $field = "user"; //Use field name for username if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered"); } else{ /* Spruce up username, check length */ $subuser = stripslashes($subuser); if(strlen($subuser) < 5){ $form->setError($field, "* Username below 5 characters"); } else if(strlen($subuser) > 30){ $form->setError($field, "* Username above 30 characters"); } /* Check if username is not alphanumeric */ else if(!eregi("^([0-9a-z])+$", $subuser)){ $form->setError($field, "* Username not alphanumeric"); } /* Check if username is reserved */ else if(strcasecmp($subuser, GUEST_NAME) == 0){ $form->setError($field, "* Username reserved word"); } /* Check if username is already in use */ else if($database->usernameTaken($subuser)){ $form->setError($field, "* Username already in use"); } /* Check if username is banned */ else if($database->usernameBanned($subuser)){ $form->setError($field, "* Username banned"); } } /* Password error checking */ $field = "pass"; //Use field name for password if(!$subpass){ $form->setError($field, "* Password not entered"); } else{ /* Spruce up password and check length*/ $subpass = stripslashes($subpass); if(strlen($subpass) < 4){ $form->setError($field, "* Password too short"); } /* Check if password is not alphanumeric */ else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){ $form->setError($field, "* Password not alphanumeric"); } } /* Email error checking */ $field = "email"; //Use field name for email if(!$subemail || strlen($subemail = trim($subemail)) == 0){ $form->setError($field, "* Email not entered"); } $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if(!eregi($regex,$subemail)){ $form->setError($field, "* Email invalid"); } $subemail = stripslashes($subemail); if($database->emailTaken($subemail)){ $form->setError($field, "* Email already in use"); } I'm not sure if the following code is correct, as its a drop down menu, more than a field. Could you help me out?, and I know where I'd put it though. $field = "location"; if(!$sublocation || strlen($sublocation = trim($sublocation)) == 0){ $form->setError($field, "* Location not selected"); Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799928 Share on other sites More sharing options...
redarrow Posted April 3, 2009 Share Posted April 3, 2009 sorry tell me in detail bro, sorry mate.. what you want? what dont work? what it for. like i say sorry. Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-799986 Share on other sites More sharing options...
JeanieTallis Posted April 3, 2009 Author Share Posted April 3, 2009 Nothing doesn't work yet, I added a location dropdown menu to the register form. The first option in the menu, is blank, that one cant be chosen, so if it's set as default, error returns 'location not selected' It needs to be in the whole code I sent, so that it checks for errors basically. This code below $field = "location"; if(!$sublocation || strlen($sublocation = trim($sublocation)) == 0){ $form->setError($field, "* Location not selected"); is a textfield error, so if nothings typed in, it recieves the error, I don't know what to do about a dropdown. Do you understand now? thanks Quote Link to comment https://forums.phpfreaks.com/topic/152306-unexpected-t_else-errors/#findComment-800116 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.