douyihuaimao733955 2013-07-02 20:24
浏览 47
已采纳

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

    }
  • 写回答

2条回答 默认 最新

  • duanmeng9336 2013-07-03 01:29
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记