Jump to content

Strange PHP setup issues


Lyleyboy

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/135195-strange-php-setup-issues/
Share on other sites

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";
}
?

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.

Archived

This topic is now archived and is closed to further replies.

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