Lyleyboy Posted December 2, 2008 Share Posted December 2, 2008 I have a weird (or maybe not) issue. I'm trying to do a really basic bit of form validation. I'm using a friends server as it's his site. I've tested this on mine 1and1 in the UK. It works fine. Tried it on his, fasthosts and it fails. The crux of the problem seems to be that I am setting a variable inside an if statement. If I then try to use the variable outside the if I get an error undefined variable. I have even tried to define the variable as a dummy value before the if statement but I get the same error. My code looks like this if ($_POST['name']<""){ $errors = "1"; $name_error = "You did not enter your name!"; } if ($errors == "1") { echo "Errors"; } The error onscreen is below. Line numbers will be wrong as I have not published all the rubbish here. Notice: Undefined variable: errors in E:\domains\n\nicholaskirtonphotography.co.uk\user\htdocs\contact_process.php on line 35 Can anyone help as I am about to move the site to a different server. P.s. Fasthosts seem to think this is fine. Quote Link to comment Share on other sites More sharing options...
gevans Posted December 2, 2008 Share Posted December 2, 2008 <?php if (!isset($_POST['name']) || empty($_POST['name'])){ $errors = "1"; $name_error = "You did not enter your name!"; } if ($errors == "1") { echo "Errors"; } ?> Quote Link to comment Share on other sites More sharing options...
Lyleyboy Posted December 2, 2008 Author Share Posted December 2, 2008 Well there we are then. That easy huh? So that worked perfectly. My only problem is the understanding. Why didn't it work initially? Many many thanks Quote Link to comment Share on other sites More sharing options...
gevans Posted December 2, 2008 Share Posted December 2, 2008 OK, let me try and explain <?php //YOUR CODE // if the variable $_POST['name'] is less than an empty string // this is not a valid if statement. <, >, <=, >= are for use with integers (numbers). if ($_POST['name']<""){ $errors = "1"; $name_error = "You did not enter your name!"; } if ($errors == "1") { echo "Errors"; } ?> Also you should add one more thing that I missed; <?php if (!isset($_POST['name']) || empty($_POST['name'])){ $errors = 1; $name_error = "You did not enter your name!"; } else $errors = 0; if ($errors) { echo "Errors"; } ? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2008 Share Posted December 2, 2008 If the post variable has something in it, that code will still give an undefined $errors message when if ($errors == "1") is executed. Before that whole block of code, set $errors to the non-error value (0, false,...) and/or use isset($errors) in the test. Quote Link to comment Share on other sites More sharing options...
gevans Posted December 2, 2008 Share Posted December 2, 2008 yea, i changed that Quote Link to comment 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.