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

Link to comment
Share on other sites

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.

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.