我添加了
<script type="text/javasript" src="<?=base_url()?>js/jquery-1.7.2.min.js"></script>
在application/views/templatess/header.php并使用我写的 jQuery将数据发布到Controller
<script>
$(document).ready(function(){
$('#submit').click(function() {
$.ajax({
type : 'POST',
url : '/projects/create',
data: {
pro_name : $('#pro_name').val()
},
success:function (data) {
$("#log_msg").html(data);
}
});
});
});
</script>
在我的视图文件中:application/views/project/create.php并且控制器存在于application/controllers/projects.php
在我看来,名为create.php的完整代码是:
<label for="pro_name">Project Name</label>
<input type="input" id="pro_name" name="pro_name" />
<br />
<input type="submit" id="submit" name="submit" value="Create" />
<script>
$(document).ready(function(){
$('#submit').click(function() {
$.ajax({
type : 'POST',
url : '/projects/create',
data: {
email : $('#pro_name').val()
},
success:function (data) {
$("#log_msg").html(data);
}
});
});
});
</script>
我的控制器名为项目包含
class Projects extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('projects_model');
$this->load->helper('url');
$this->load->helper('html');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function create()
{
echo $this->input->post('pro_name');
$data['title'] = 'SPARCS | Create Project';
$this->form_validation->set_rules('pro_name', 'Name', 'required');
$this->form_validation->set_rules('pro_client', 'Client', 'required');
$this->form_validation->set_rules('pro_loc_city', 'City', 'required');
$this->form_validation->set_rules('pro_loc_state', 'State', 'required');
$this->form_validation->set_rules('pro_size', 'Project Size', 'required');
$this->form_validation->set_rules('pro_desc', 'Description', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('projects/create');
$this->load->view('templates/footer');
}
else
{
// If project name already exists?
$query = $this->projects_model->if_exists($this->input->post('pro_name'));
if ( sizeof($query) == 0) {
//$this->projects_model->set_project();
$data['message'] = 'Add successfully';
} else {
$data['message'] = 'Project name already exists';
}
$this->load->view('projects/log_message', $data);
}
}
}
但是浏览器说$ is not defined
现在请让我知道如何在CodeIgniter中设置 jQuery,以及将数据传递给控制器并显示从控制器发回的响应的正确方法是什么