将一个html表单链接到基于CodeIgniter框架的phpapi


Linking an html form to a php api based off of CodeIgniter framework?

所以我正在为学校创建一个无法添加用户的web应用程序。它是一个仅限会员的网站,因此有一个登录功能可以检查登录的用户是否是数据库中的有效用户。我在获取html登录表单(login.html(以成功连接我们的API(login_controller.php(时遇到问题。目前的主要问题是表单操作中的链接会显示localhost/application/controller/login_controllerphp(不存在(,而不是显示localhost/CMPS285_Team1/SGA connect/application/concontrollers/login_controler.php请帮助!以下是两个文件:

login.html--

<html>
<head>
    <link rel="databaseandverification" href="/application/controllers/login_controller.php"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../css/login/reset.css">
    <link rel='stylesheet prefetch' href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700,900|RobotoDraft:400,100,300,500,700,900'>
    <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'>
    <link rel="stylesheet" href="../css/login/login1.css">

</head>
<body>
    <div class="pen-title">
        <div style="text-align: center;"><img src="../images/finalsgaconnectlogo.png" alt="SGA Connect" width="" height=""></div>
    </div>
    <!-- Form Module-->
    <div class="module form-module">
        <h2><div style="text-align:center">Login</div></h2>
        <form action="/application/controllers/login_controller.php" method="post">
            <fieldset>
                <!--<input type="text" name="username" class="form-control" placeholder="Username" /> -->
                <label for="username">Username</label>
                <input type="text" id="username" name="username" value="" maxlength="50" />
                <label for="password">Password</label>
                <input type="password" id="password" name="password" value="" maxlength="32"/>
                <input type="submit" name="submit" value="Login"/>
            </fieldset>
        </form>

login_controller.php--

<?php
class Login_controller extends CI_Controller
{
    function validate(){
//       when posting  json data to the api
        $json = key($this->input->post(NULL, TRUE));
        $json = json_decode($json);
        $username = $json->username;
        $password = md5($json->password);
//        when posting with a normal post method with parameters
//        $username = $this->input->post('username');
//        $password = md5($this->input->post('password'));
        $this->load->model('membership_model');
        $query = $this->membership_model->validate($username,$password);
        if($query) {
            $data['status'] = 'Logged in';
        }
        else{
            $data['status'] = 'not logged in ';
        }
        $this->load->view('verify', $data);
    }
}

不能用这种方式调用文件。表单操作需要http请求字符串,例如:

<form action="http://localhost/project/index.php/login_controller/validate" method="post">

如果你加载url助手,你可以用以下方式调用它:

<form action="<?php echo base_url('login_controller/validate'); ?>http://localhost/project/index.php/login_controller/validate" method="post">

在此之前,您希望在APPPATH . 'config/autoload.php'文件中自动加载url帮助程序。

此外,如果您不包括视图文件的扩展名(即$this->load->view('verify', $data);(,CodeIgniter将在视图目录中查找verify.php文件。否则,你必须提到扩展,比如:

$this->load->view('verify.html', $data);

但我不确定您是否能够读取其中的PHP变量。不过,通常的做法是使用php扩展名保存所有视图文件。

如果您使用的是CI 3.0.x版本,则控制器需要大写为Login_controller.php