在OOP PHP中提交表单后,避免需要刷新

在OOP PHP中提交表单后,避免需要刷新

问题描述:

When i use a MVC framework in OOP PHP, and i submit a form, i have to refresh the page to go on to the next "elseif".

Even though the function "$login->isConfigFileCreated()" now returns true (after the class constructer have been executed) it shows the same view as before, and requires a refresh to "move on".

I know for sure, that the problem isn't in the "createConfigFile() function". This is a common problem for me.

<?php

require_once("db.php");
require_once("classes/Login.php");

$login = new Login();

**if ($login->isConfigFileCreated() == false)
{
    include("views/config_file.php");
}
elseif ($login->isAdminCreated() == false)
{
    include("views/register.php");
}**
else
{
    include("views/not_logged_in.php");
}

The config_file.php view file looks like this:

<form method="post" action="" name="create_config_file">
        <input type="text" name="input_db_host" required />
        <input type="text" name="input_db_name" required />
        <input type="text" name="input_db_user" required />
        <input type="text" name="input_db_pass" required />
        <input type="submit" name="create_config_file" value="Create" />
</form>

And the class, with the functions, looks like this for example:

public function __construct()
    {
        if (isset($_POST["create_config_file"]))
        {
            $this->createConfigFile();
        }
        if (isset($_POST["register"]))
        {
            $this->register();
        }
    }
public function isAdminCreated()
    {
        $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        if (!$this->db_connection->connect_errno)
        {
            $checksetup = $this->db_connection->query("SELECT user_email FROM users WHERE user_id = '1';");
            if ($checksetup->num_rows == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }


public function isConfigFileCreated()
    {
        if(defined('DB_HOST') && defined('DB_NAME') && defined('DB_USER') && defined('DB_PASS'))
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public function createConfigFile($action)
    {
        if ($action == "complete")
        {
            $file_content = file_get_contents("db.php");
            $file_content .= 'define("CONFIG_DONE", "true");';
            file_put_contents("db.php", $file_content);
        }
        else
        {
            $this->db_connection = new mysqli($_POST['input_db_host'], $_POST['input_db_user'], $_POST['input_db_pass'], $_POST['input_db_name']);
            if (!$this->db_connection->connect_errno)
            {
                $file_content =    '<?php'."
";
                $file_content .=   'define("DB_HOST", "'.$_POST['input_db_host'].'");'."
";
                $file_content .=   'define("DB_NAME", "'.$_POST['input_db_name'].'");'."
";
                $file_content .=   'define("DB_USER", "'.$_POST['input_db_user'].'");'."
";
                $file_content .=   'define("DB_PASS", "'.$_POST['input_db_pass'].'");'."
";

                file_put_contents("db.php", $file_content);
                fclose("db.php");
                $login->messages[] = "That's it! You're now ready to kick ass at some image management!";
            }
            else
            {
                $this->errors[] = "Wrong database credentials!"; 
            }
        }

    }

The db.php is included before before the createConfigFiles function executes.

You either need to define the variables in the createConfigFiles function, as well as creating the file:

public function createConfigFile($action)
{
    if ($action == "complete")
    {
        $file_content = file_get_contents("db.php");
        $file_content .= 'define("CONFIG_DONE", "true");';
        file_put_contents("db.php", $file_content);
    }
    else
    {
        $this->db_connection = new mysqli($_POST['input_db_host'], $_POST['input_db_user'], $_POST['input_db_pass'], $_POST['input_db_name']);
        if (!$this->db_connection->connect_errno)
        {
            $file_content =    '<?php'."
";
            $file_content .=   'define("DB_HOST", "'.$_POST['input_db_host'].'");'."
";
            $file_content .=   'define("DB_NAME", "'.$_POST['input_db_name'].'");'."
";
            $file_content .=   'define("DB_USER", "'.$_POST['input_db_user'].'");'."
";
            $file_content .=   'define("DB_PASS", "'.$_POST['input_db_pass'].'");'."
";

            file_put_contents("db.php", $file_content);
            fclose("db.php");
            define("DB_HOST", $_POST['input_db_host']);
            define("DB_NAME", $_POST['input_db_name']);
            define("DB_USER", $_POST['input_db_user']);
            define("DB_PASS", $_POST['input_db_pass']);;
            $login->messages[] = "That's it! You're now ready to kick ass at some image management!";
        }
        else
        {
            $this->errors[] = "Wrong database credentials!"; 
        }
    }

}

Or you could do this if Login will never use the database on __construct:

require_once("classes/Login.php");

$login = new Login();

require_once("db.php");

Doing both is unnecessary!

The elseif will make all of the code related. If you don't want it to be related and want it to continue and check the next if statement, then just remove the else:

if ($login->isConfigFileCreated() == false)
{
    include("views/config_file.php");
}

if ($login->isAdminCreated() == false)
{
    include("views/register.php");
}

if( !$login->isConfigFileCreated() && $login->isAdminCreated())
{
    include("views/not_logged_in.php");
}

This will include the config_file.php and then check to see if the admin was created

After your input here is an updated answer:

So the problem is that the file you are writing to is db.php which has already been included in the script.

So the flow is require db.php -> then write to that file. That means that the changes you wrote to this file will not be available since the script was already loaded.

The solution here would be to not use constant variables since they cannot be changed. If you use regular variables then you can just include db.php again to override the variables that were defined before.

The other option would be to have your create config files return the values that need to be defined so that you can override what was already included earlier.