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