CodeIgniter:为什么回调函数后变量会发生变化?我以正确的方式使用它吗?
CodeIgniter: Why variable change after callback function? Am I using it the right way?
我目前正在使用 CodeIgniter。在表单验证之后,我使用函数 "set_rules" 来检查用户信息是否正确。
否则,我尝试使用 "callback" 函数发送 2 个变量,但当我使用 "callback" 函数时,第二个变量似乎改变了它的值。如果在我的表格中,我将其填写为:
Username = "test_username"
Password = "test_password"
在我的函数数据库中,
$username will display "test_username"
$password will display "test_username,test_password".
我这样试过:
function index()
{
$this->load->library('form_validation');
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->form_validation->set_rules('username', 'Username', 'trim|required', 'wrong or missing username');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database($username, $password)', 'wrong or missing password');
}
function check_database()
{
echo '$password'. '</br>'; //display => test_username
echo '$username'. '</br>'; // display => test_username,test_password
}
我尝试用以下代码替换几行高级代码:
function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required', 'wrong or missing username');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database['. $this->input->post($username). ','. $this->input->post($password), 'wrong or missing password'];
function check_database($password, $username)
{
echo '$password'. '</br>'; //display => test_username
echo '$username'. '</br>'; // display => test_username,test_password
}
但这是同样的问题。
我没有在 CodeIgniter 站点上找到回调函数的手册。我的第二个问题是当我写
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database', 'wrong or missing password');
function check_database() //work only if I write check_database($password)
{
//blah blah blah
}
它弹出一个错误。鉴于我没有找到 call_back 函数的任何手册,我想回调函数用于测试密码变量的 set_rules 所以我认为 call_back 函数会自动将密码变量发送到 check_database() 函数。(这就是为什么我需要将 $password 放入 check_database 原型)。
我已经找到了解决方案,但我只是想知道会发生什么(我很好奇)?
有谁能告诉我为什么在第一个和第二个代码中,回调的第二个参数一旦在 check_database() 上就会改变?
顺便说一句,你能确认一下我最后的代码是否正确吗?更准确地说,当我说 call_back 函数会自动将密码变量发送到 check_database() ?
谢谢
PS: 在我之前给你看的代码中,我自愿删除了一部分代码以避免你阅读太多,因为我认为post有点长
变量或值没有改变。在 codeigniter 表单验证中,回调第一个参数提供值。
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database[x]');
....
function check_database($str, $param1)
{
echo $str; // password
echo $param1; // x
}
如果您想提供其他输入 post 参数,这更容易:
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
function check_database($str)
{
$username = $this->input->post('username'); // same input post value
....
}
希望对您有所帮助。
我目前正在使用 CodeIgniter。在表单验证之后,我使用函数 "set_rules" 来检查用户信息是否正确。 否则,我尝试使用 "callback" 函数发送 2 个变量,但当我使用 "callback" 函数时,第二个变量似乎改变了它的值。如果在我的表格中,我将其填写为:
Username = "test_username"
Password = "test_password"
在我的函数数据库中,
$username will display "test_username"
$password will display "test_username,test_password".
我这样试过:
function index()
{
$this->load->library('form_validation');
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->form_validation->set_rules('username', 'Username', 'trim|required', 'wrong or missing username');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database($username, $password)', 'wrong or missing password');
}
function check_database()
{
echo '$password'. '</br>'; //display => test_username
echo '$username'. '</br>'; // display => test_username,test_password
}
我尝试用以下代码替换几行高级代码:
function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required', 'wrong or missing username');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database['. $this->input->post($username). ','. $this->input->post($password), 'wrong or missing password'];
function check_database($password, $username)
{
echo '$password'. '</br>'; //display => test_username
echo '$username'. '</br>'; // display => test_username,test_password
}
但这是同样的问题。 我没有在 CodeIgniter 站点上找到回调函数的手册。我的第二个问题是当我写
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database', 'wrong or missing password');
function check_database() //work only if I write check_database($password)
{
//blah blah blah
}
它弹出一个错误。鉴于我没有找到 call_back 函数的任何手册,我想回调函数用于测试密码变量的 set_rules 所以我认为 call_back 函数会自动将密码变量发送到 check_database() 函数。(这就是为什么我需要将 $password 放入 check_database 原型)。
我已经找到了解决方案,但我只是想知道会发生什么(我很好奇)?
有谁能告诉我为什么在第一个和第二个代码中,回调的第二个参数一旦在 check_database() 上就会改变? 顺便说一句,你能确认一下我最后的代码是否正确吗?更准确地说,当我说 call_back 函数会自动将密码变量发送到 check_database() ?
谢谢
PS: 在我之前给你看的代码中,我自愿删除了一部分代码以避免你阅读太多,因为我认为post有点长
变量或值没有改变。在 codeigniter 表单验证中,回调第一个参数提供值。
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database[x]');
....
function check_database($str, $param1)
{
echo $str; // password
echo $param1; // x
}
如果您想提供其他输入 post 参数,这更容易:
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
function check_database($str)
{
$username = $this->input->post('username'); // same input post value
....
}
希望对您有所帮助。