MesiaH Posted December 20, 2011 Share Posted December 20, 2011 Still new to PHP, trying to get around an error! Scenario is pretty simple, i'm trying to do a sample form validation via php, this is the code, <?php if($_POST){ $t_name=trim($_POST["name"]); $t_email=trim($_POST["email"]); if($t_name==""){ $n_err=1; } elseif($t_email==""){ $e_err=1; } } ?> then towards the form fields i do this, <?php if($n_err)echo "Please enter correct name"."</br"; ?> <?php if($e_err)echo "Please enter correct email"."</br>"; ?> But this gives an error Notice: Undefined variable: n_err in C:\wamp\www\php\form\index.php on line 30 As far i see you can use php vars defined in section on others, only var inside function are local in nature if they are not using "global" prefix. Please help me figure out why im getting this error, i know it might be something simple, so kindly help, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/253535-getting-undefined-variable-error/ Share on other sites More sharing options...
SergeiSS Posted December 20, 2011 Share Posted December 20, 2011 Well... It's true. Check your if-else counstruction. If $t_name is not empty, then $n_err will be undefined variable. Also, in the beginning this comparison if( $_POST) is incorrect! What are you checknig? Nothing. You'd check like here if( isset( $_POST['any_submit'] ) {.... } // the next line $t_name=trim($_POST["name"]); // you'd change to $t_name=isset( $_POST['name'] ? trim($_POST['name']) : ''; // and forget about $n_err; just check if( $t_name <> '' ) ..... // any code Quote Link to comment https://forums.phpfreaks.com/topic/253535-getting-undefined-variable-error/#findComment-1299656 Share on other sites More sharing options...
MesiaH Posted December 20, 2011 Author Share Posted December 20, 2011 Well... It's true. Check your if-else counstruction. If $t_name is not empty, then $n_err will be undefined variable. Also, in the beginning this comparison if( $_POST) is incorrect! What are you checknig? Nothing. You'd check like here if( isset( $_POST['any_submit'] ) {.... } // the next line $t_name=trim($_POST["name"]); // you'd change to $t_name=isset( $_POST['name'] ? trim($_POST['name']) : ''; // and forget about $n_err; just check if( $t_name <> '' ) ..... // any code Thanks bro, for the quick reply! I changed the code and initialized the '$n_err' and '$e_err' to 0 and then checked if they are 1 to echo the error. However that doesn't work either. The edited code is blelow, if($_POST){ $t_name=trim($_POST["name"]); $t_email=trim($_POST["email"]); $n_err=0; $e_err=0; if($t_name==""){ $n_err=1; //echo "Please enter a name"."</br>"; } elseif($t_email==""){ $e_err=1; //echo "Please enter an email"."</br>"; } } ?> <?php if($n_err==1)echo "Please enter correct name"."</br"; ?> <?php if($e_err==1)echo "Please enter correct email"."</br>"; ?> Regarding the $_POST checking, i was checking if the _POST var would have any value! if _POST has any value then the form has been submitted and If($_POST) would return true! ain't that right? Quote Link to comment https://forums.phpfreaks.com/topic/253535-getting-undefined-variable-error/#findComment-1299659 Share on other sites More sharing options...
SergeiSS Posted December 20, 2011 Share Posted December 20, 2011 "if the _POST var would have any value" == if( count( $_POST) ) but not that you did. And.... It seems that you didn't see the part of my answer concerning variable's assignments I mean that this code $t_name=isset( $_POST['name'] ? trim($_POST['name']) : ''; is better than you use. Quote Link to comment https://forums.phpfreaks.com/topic/253535-getting-undefined-variable-error/#findComment-1299682 Share on other sites More sharing options...
MesiaH Posted December 24, 2011 Author Share Posted December 24, 2011 "if the _POST var would have any value" == if( count( $_POST) ) but not that you did. And.... It seems that you didn't see the part of my answer concerning variable's assignments I mean that this code $t_name=isset( $_POST['name'] ? trim($_POST['name']) : ''; is better than you use. Sorry for the late reply bro, yeah you're right! Regarding checking of $_POST var i was just looking for something that works, but your's is more more efficient, i should say! and yeah don't need another error flag, can just use the construct you used. Seems like i could reduce the number of loc even more, thanks bro! will keep asking more q? forever.. Quote Link to comment https://forums.phpfreaks.com/topic/253535-getting-undefined-variable-error/#findComment-1301055 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.