Welcome Guest, Not a member yet? Register   Sign In
Passing form data
#1

[eluser]bcarter[/eluser]
Hi

Noob here, I'm trying to do a simple search page with a results page, it's all working except the most important part....querying the database of the keyword.

Here is the Search controller

Code:
<?php

class Search extends Controller {
    
    function Search() {
        
        parent::Controller();
    }
    
    function index() {
        $data['page_title'] = "Search the database";
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('keyword', 'Keyword', 'required');
        
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('search_view', $data);
        } else {
            redirect('results');
        }
    }
}

Here is the Search_view

Code:
<?php echo form_open('search') ?>
<p>&lt;?php echo form_input('keyword',set_value('keyword')); ?&gt; &lt;?php echo form_submit('submit','Search'); ?&gt;</p>
<p>&lt;?php echo form_error('keyword'); ?&gt;</p>
&lt;/form&gt;

....and the Result Model

Code:
&lt;?php

class Results_model extends Model {
    
    function Results_model() {
        
        parent::Model();
    }
    
    function get_results($keyword) {
    
        $this->db->like('empName', $keyword);
        $this->db->or_like('contact', $keyword);
        $this->db->or_like('linkAdviser', $keyword);
        $this->db->or_like('leadSource', $keyword);
        
        $q = $this->db->get('details');
        
        return $q->result();
        
    }
}

...the Result controller

Code:
&lt;?php

class Results extends Controller {
    
    function Results() {
        
        parent::Controller();
    }
    
    function index() {
        
        $keyword = $this->input->post('keyword');
        $data['page_title'] = "Search Results";
        $this->load->model('Results_model');
        $data['q'] = $this->Results_model->get_results($keyword);
        $this->load->view('head', $data);
        $this->load->view('results_view', $data);
    }
}

and finally the result view

Code:
&lt;?php foreach ($q as $row):?&gt;
    
    <h3><a href="">&lt;?=$row->empName ?&gt;</a></h3>
        
    <strong>Contact: &lt;?=$row->contact ?&gt;</strong><br />
    
    <strong>Tel: &lt;?=$row->tel ?&gt;</strong><br />
    
    <strong>Email: &lt;?=$row->email ?&gt;</strong><br />
    
    <strong>Area of Interest: &lt;?=$row->interestArea ?&gt;</strong><br />
    
    <strong>Lead assigned to: &lt;?=$row->linkAdviser ?&gt;</strong><br />
    
    <strong>Lead source: &lt;?=$row->leadSource ?&gt;</strong><br />
    
    <strong>Lead Status: &lt;?=$row->lead_status ?&gt;</strong><br /><br />
    
&lt;?php endforeach; ?&gt;

When I click 'Search' it performs a query without a keyword and so returns all records from the database. The way I'm thinking is that it is not picking up the 'keyword' and assigning it to the $keyword variable in the results controller.

Can somebody point me in the right direction? Please remember I'm a CodeIgniter noob.

Many thanks for your assistance!!!
#2

[eluser]rogierb[/eluser]
Hi,

when you redirect, you loose the $_POST data. So you have no keyword.

Instead of using a new controller, why not doe something like:
Code:
if ($this->form_validation->run() == FALSE) {
            $this->load->view('search_view', $data);
        } else {
             $keyword = $this->input->post('keyword');
             $data['page_title'] = "Search Results";
             $this->load->model('Results_model');
             $data['q'] = $this->Results_model->get_results($keyword);
             $this->load->view('head', $data);
             $this->load->view('results_view', $data);
        }
in your search controller?
#3

[eluser]bcarter[/eluser]
About 30 secs before I read your post I figured out that I was loosing the $_POST data.

Thanks for your response!




Theme © iAndrew 2016 - Forum software by © MyBB