我已经调试这段代码好几个小时了,但没有成功。我所要做的就是加载一个视图。我没有加载,而是得到了一个白色屏幕,任何地方都没有错误消息。
这是来自控制器的代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}
//echo "we made it to the function";
public function login_form()
{
$this->load->view('login_form');
}
public function login_submit()
{
print_r( $_POST );
$this->load->model('Usermodel', 'users');
$match = $this->users->authenticate_user( $_POST['email'], $_POST['password'] );
if( $match )
{
echo "User exists in database!";
}
else
{
echo "Email or password is wrong, bretheren!";
}
}
}
重要的部分是"函数login_form"。这是该视图的代码:
<form action="<?php base_url() ?>welcome/login_submit" method="post">
<label>
email:
<input id="email" name="email" />
</label>
<label>
password:
<input type="password" id="password" name="password" />
</label>
<input type="submit" value="Log me in!" />
</form>
这是我在浏览器中用于访问页面的链接:localhost/intranet/index.php/welcome/login_form
代码对我来说都很好,我只是不知道程序在哪里坏了。有人有什么想法吗?
编辑:我去掉了速记,但我也有同样的问题。
1)您缺少一个echo
。。。
<?php echo base_url() ?>
2) 要使用base_url()
,还需要将URL帮助程序加载到某个位置。
https://www.codeigniter.com/user_guide/helpers/url_helper.html
3) 我强烈建议阅读整个文档,包括简单的演示&教程,在启动CodeIgniter项目之前。。。
https://www.codeigniter.com/user_guide/
4) 尽管您可以使用PHP短标签(根据您的服务器配置),但不建议使用它们。
https://www.codeigniter.com/user_guide/general/styleguide.html#short-打开标签
如果您想使用base_URL()函数,您必须在配置中或在控制器中启用URL Helper:
$this->load->helper('URL');
<form action="<?php base_url() ?>welcome/login_submit" method="post">
应该是
<form action="<?php echo base_url() ?>welcome/login_submit" method="post">
或者,如果您正确配置了它,那就是短兵相接:
<form action="<?=base_url() ?>welcome/login_submit" method="post">
在帖子中抱怨不能使用CodeIgniter速记,是的,你可以,我刚刚完成了一个使用速记的项目。这就是如何配置php.ini
<?= is just short for <?php echo
您正在使用codeigniter,为什么不使用它的全部功能呢。你可以在代码点火器中创建这样的表单
因此,您不需要使用base_url左右,这是在codeigniter中使用的正确方法。
<?php echo form_open("welcome/login_submit",array('method'=>'post')); ?>
<label>
email:
<?php echo form_input(array('name'=>'email','id'=>'email')); ?>
</label>
<label>
password:
<?php echo form_password(array('name'=>'password','id'=>'password')); ?>
</label>
<?php
echo form_button(array('type'=>'submit','value'=>'Log me in!'));
echo form_close(); ?>