当我的控制器功能加载视图时,URL的控制器名称和功能仍在URL中。我怎么能摆脱这个?

时间:2014-04-17 21:27:17

标签: php codeigniter codeigniter-url codeigniter-routing

基本上我的函数会检查数据库中是否有东西。如果发现它返回true并继续下一个视图。但是,如果它返回false,则重新加载home.php视图,但它似乎仍然在URL中留下URI段...

example:
www.example.com/index.php/home/checkSearchFields

如何让它摆脱'home / checkSearchFields'?

感谢您的帮助。

home.php(查看)

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script type="text/javascript">
    var base_url = window.location.origin;
    function getStates(value) {
        $.post(base_url + "/buildings/getstates.php",{partialState:value},function(data){
            $("#results").html(data);
        });
    }

    function myFunction(){
        var target = event.target || event.srcElement;
        var str = target.innerHTML;
        console.log(str);
        document.getElementById("stateSearch").value = str ;
        $( "#results" ).empty();
    }

</script>
</head>
<body>
<div id="container">

<h1>Room Finder</h1>

<?php echo validation_errors(); ?>

<?php echo form_open('home/checkSearchFields'); ?>

    <div class="searchfield">

        Building: <br/>
        <input type="text" name="building" onkeyup="getStates(this.value)" id="stateSearch" autocomplete="off"/>

    </div><!-- closes searchfield -->
    <div class="searchfield">

        Room #: <br/>
        <input type="text" name="roomNum" autocomplete="off"/><br/>

    </div><!-- closes searchfield -->
    <div class="submit">

        <input type="submit" value="Submit" name="submit" />

    </div><!-- closes submit -->

</form>

<div id="results"></div>

home.php(控制器)

class home extends CI_Controller {

public function index()
{
    $this->load->view('home');
}

public function checkSearchFields(){

    $this->form_validation->set_rules('building', 'Building', 'required|callback_verifyBuilding');
    $this->form_validation->set_rules('roomNum', 'RoomNum', 'required');

    $roomNumber = $this->session->userdata('roomNumber'); // going to be checked to see if null

    if($this->form_validation->run() == false){
        $this->load->view('home');
    }else{
        $this->load->view('worked');
    }


}



public function verifyBuilding(){

    $buildings = $this->input->post('building');
    $roomNum = $this->input->post('roomNum');

    $this->load->model('BuildingModel');

    if($this->BuildingModel->userSearch($buildings, $roomNum)){
        return true;
    }else{
        $this->form_validation->set_message('verifyBuilding', 'Incorrect Building Name or Room Number... Please try again...');
        return false;
    }
}


}

buildingmodel.php(模型)

class BuildingModel extends CI_Model{
public function userSearch($buildings, $roomNum){


    $this->db->select('*');
    $this->db->from('buildings');
    $this->db->where('buildingName', $buildings);

    $query = $this->db->get();




    if($query->num_rows() == 1){

        foreach($query->result_array() as $row)
        {
            $buildingID = $row['buildingID'];
            $buildingName = $row['buildingName'];
            $buildingLocation = $row['buildingLocation'];
            $imagePath = $row['imagePath'];
        }

        $userSearch = array(
               'buildingID' => $buildingID,
               'buildingName' => $buildingName,
               'buildingLocation' => $buildingLocation,
               'imagePath' => $imagePath,
           );

        $this->session->set_userdata($userSearch);
        //query DB for the room numbers


            $this->db->select('*');
            $this->db->from('rooms');
            $this->db->where('buildingID', $buildingID);
            $this->db->where('roomNumber', $roomNum);

            $query2 = $this->db->get();

            if($query2->num_rows() == 1){

                foreach($query2->result_array() as $row)
                    {
                        $roomID = $row['roomID'];
                        $roomNumber = $row['roomNumber'];
                        $roomCode = $row['roomCode'];
                        $office = $row['office'];
                        $location = $row['location'];
                        $roomName = $row['roomName'];
                    }

                    $userSearch = array(
                           'roomID' => $roomID,
                           'roomNumber' => $roomNumber,
                           'roomCode' => $roomCode,
                           'office' => $office,
                           'location' => $location,
                           'roomName' => $roomName,
                       );
                    $this->session->set_userdata($userSearch);
            }


        return true;
    }else{
        return false;
    }
}

}

1 个答案:

答案 0 :(得分:0)

正如你所说,它只是加载视图。加载了哪个视图,与URL的外观无关。

您可以替换它:

 if($this->form_validation->run() == false){
    $this->load->view('home');
}else{
    $this->load->view('worked');
}

用这个:

 if($this->form_validation->run() == false){
    redirect('home');
}else{
    $this->load->view('worked');
}

如果失败,它会将您重定向到主页。