我的PHP(CodeIgniter)网站上有一个搜索功能。但我想在网址中传递查询字词,以便Google Analytics可以跟踪该信息。我认为这是基本的,但我只是学习如何编码。
这是我的HTML:
<form class="navbar-form navbar-right mtop10 mright10 hidden-xs" role="search" method="post" action="<?php echo base_url('cursos/searchCourse'); ?>">
<div class="form-group">
<input class="form-control" type="text" name="search_course" placeholder="Busque um curso" />
<input type="hidden" name="url" value="<?php echo current_url(); ?>" />
</div>
<button class="btn btn-md btn-default" type="submit" value="">
<span class="glyphicon glyphicon-search"></span>
</button>
<?php if($this->session->flashdata('search_error')) : ?>
<?php $errors = $this->session->flashdata('search_error');echo $errors['search_course']; ?>
<?php endif; ?>
</form>
这是函数searchCourse:
public function searchCourse(){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p class="alert alert-danger mtop5" role="alert">', '</p>');
$this->form_validation->set_rules('search_course', 'Busca', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_message('required', 'Campo de preenchimento obrigatório');
$this->form_validation->set_message('min_length', 'O campo precisa conter pelo menos 2 caracteres');
if ($this->form_validation->run()){
$search = $this->curso_model->getCoursesBySearch($this->input->post('search_course'));
if(!empty($search)){
$this->index($search);
}else{
$this->set_flashdata('search_empty', 'search_empty', $this->input->get('url'));
}
}else{
$errors = array('search_course'=>form_error('search_course'));
$this->set_flashdata('search_error', $errors, $this->input->get('url'));
}
}
这是我的模特:
public function getCoursesBySearch($search){
$this->db->select('*');
$this->db->from('curso');
$this->db->group_by('curso_nome');
$this->db->like('curso_nome', $search);
//$this->db->or_like('curso.curso_descricao', $search);
//$this->db->or_like('curso.curso_categoria', $search);
$this->db->or_like('tags.tags_nome', $search);
$this->db->join('curso_tags', 'curso_tags.curso_id = curso.curso_id');
$this->db->join('tags', 'tags.tags_id = curso_tags.tags_id');
//$this->db->where('curso_status', 1);
//$this->db->distinct();
$this->db->distinct();
$query = $this->db->get();
print_r($search);
//exit;
return $query->result();
}
有人能给我一些线索吗?
干杯!
答案 0 :(得分:1)
使用jquery动态修改action
属性。将id
attritube分配给每个表单元素,包括表单本身
HTML:
<form id="my_form" method="post" action="dummy.html">
<input id="search_text" type="text" name="search_course" placeholder="Busque um curso" />
<input type="hidden" name="url" value="sdff" />
<button id="my_button" type="submit" value="Submit"> </button>
</form>
Jquery:(在表单下方添加)
<script>
$('#my_button').click(function(){
var search_text = $("#search_text").val();
// Replace my_search.php with actual landing page
$('#my_form').attr('action', 'my_search.php' + '?search=' + search_text);
});
</script>
注意:您的搜索文本将以URL(如果是空格等)进行解码,并且您已在着陆页上解码了该网址。
答案 1 :(得分:0)
将方法从post更改为get,因此值将显示在URL中:
<form class="navbar-form navbar-right mtop10 mright10 hidden-xs" role="search" method="post" action="<?php echo base_url('cursos/searchCourse'); ?>">
替换为:
<form class="navbar-form navbar-right mtop10 mright10 hidden-xs" role="search" method="get" action="<?php echo base_url('cursos/searchCourse'); ?>">