Jump to content

Getting undefined variable error!


MesiaH

Recommended Posts

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!

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

"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..:P

 

Link to comment
Share on other sites

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.