0

I've been trying to make a setup form for a Database, however I've run into a problem where I keep constantly getting a notice undefined index on my variables. My form is:

if(!isset($dbServer)) {
    echo "
        <form method='POST'>
        <div class='formHolder'>
            <h2>Create the MySQL structure</h2>
            <h3>
                Input the address of the SQL server
            </h3>
            <label>SQL Server: </label>
            <input type='text' name='dbServer' placeholder='e.g. localhost' />
    ";
}

if (!isset($dbUsername) && !isset($dbPassword)) {
    echo "
        <h3>
            Input the login to the Database so DIY CMS can connect
        </h3>

        <label>SQL Database Username: </label>
        <input type='text' name='dbUsername' placeholder='e.g. admin' />
        <br />
        <label>SQL Database Password: </label>
        <input type='password' name='dbPassword' placeholder='e.g. root' />
        <br />
        <input type='submit' name='submit' value='Done' />
        </div>
        </form>
    ";
}

And then underneath that I have variables from the post:

$dbServer = $_POST['dbServer'];
$dbUsername = $_POST['dbUsername'];
$dbPassword = $_POST['dbPassword'];

After the variables there are a few if statements and some more PHP that uses the variables, but every single time I get undefined index as soon as I load the page, and I'd rather not have them appear.

My guess is that PHP is running through all the code and then sees that my variables are undefined since the POST hasn't happened yet. But I'm not sure, any explanation as to why I'm getting the errors on my variables and potential solutions are appreciated.

user229044
  • 232,980
  • 40
  • 330
  • 338
ParadAUX
  • 53
  • 1
  • 8
  • How many times has this been answered alone here on SO? 6583483648 times? – arkascha Jan 31 '17 at 10:33
  • It's great that you're so fast to reply, but none of the other solutions have worked for me. So yeah a bunch of times that didn't work, and I don't want to just hide errors, because that's sloppy, I want it to not give the errors at all. – ParadAUX Jan 31 '17 at 10:34
  • Your own explanation is exactly right and what is to be expected. The main issue is that you are trying to define the form and the form processing code _in the same file_ which simply causes all sorts of issues. Don't! Place them in different files, keep separate things separate. – arkascha Jan 31 '17 at 10:34
  • "none of the other solutions have worked for me"... Strange.... – arkascha Jan 31 '17 at 10:35
  • TO avoid that error before assigning the $_POST data just check once the require index exist in $_POST variable or not. For that you can use ```array_key_exists()``` . – Suresh Jan 31 '17 at 10:40

0 Answers0