gpt4 book ai didi

php - 将参数从 Controller 传递到 codeigniter 中查看

转载 作者:行者123 更新时间:2023-11-28 18:31:13 26 4
gpt4 key购买 nike

我是 code Igniter 的新手,所以我不知道该怎么做。所以我的问题是我有一个从 ajax 提交的表单。所以我想做的是当表单成功提交时,通知或 css div 类将出现在表单上方,然后消失。我不知道如何在接受从 View 页面到 Controller 的参数后执行此操作我不知道如何发送参数 Controller 来查看或如何执行所有这些操作。这是我的 Controller :

   class categoryController extends CI_Controller {


function index(){

$data['main_content'] = 'categoryView';
$this->load->view('dashboardTemplate/template',$data);

}


function addCategory(){

//getting parameters from view
$data = array(
'cat_name' => $this->input->post('cat_name')

);

$is_ajax = $this->input->post('ajax'); //or use this line


$this->load->model('categoryModel');
$query = $this->categoryModel->addCategories($data);


if ($query && $is_ajax){


$page['main_content'] = 'categoryView';
$page['v'] = '1'; // i dont know how this variable is not accessing in view page by echo $v
$this->load->view('dashboardTemplate/template',$page);


}
else
{
//
}
}}

这是我的看法:

     <?php 
$attributes = array('id' => 'form-horizontal',
'class' => 'form-horizontal'

);
echo form_open('categoryController/addCategory', $attributes);


$cat_name = array(
'name' => 'cat_name',
'id' => 'cat_name',
'class' => 'cat_name');
$button = array(
'name' => 'button',
'id' => 'btn',
'class' => 'btn btn-primary',
'value' => 'submit',
'type' => 'submit',
'content' => 'Submit'


);
?>
<h3>Add Category</h3>

//here i want to do this .. that if form is submitted succesfully then this class will load only and the whole page remain the same


<div> class="alert-heading">sucess or not success!<div>

</div>
<div class="control-group">
<label for="basicround" class="control-label">Category Name:</label>
<div class="controls">
<?php echo form_input($cat_name); ?>
<div class="form-actions">
<?php echo form_button($button); ?></div>



<script type="text/javascript">
$('#btn').click(function() {

var cat_name = $('#cat_name').val();

if (!cat_name || cat_name == 'Name') {
alert('Please enter Category Name');
return false;
}

var form_data = {
cat_name: $('#cat_name').val(),
ajax: '1'

};

$.ajax({
url: "<?php echo site_url('categoryController/addCategory'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
$('#message').html(msg);
}
});

return false;
});

</script>

最佳答案

由于是Ajax提交,所以需要从controller传JSON数组给view

echo json_encode($page);

Controller

$page['main_content'] = 'categoryView';
$page['v'] = '1'; // i dont know how this variable is not accessing in view page by echo $v
echo json_encode($page);

对于上面的步骤,需要定义

data-type:JSON

在您的 ajax 函数中。

Ajax 函数

$.ajax({
url: "<?php echo site_url('categoryController/addCategory'); ?>",
type: 'POST',
data: form_data,
data-type: "json",
success: function(msg) {
// Here you can access the values from controller like msg.v
$('#message').html(msg);
}
});

根据响应,您可以使用以下方式显示成功消息

$(".alert-heading").show();

关于php - 将参数从 Controller 传递到 codeigniter 中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14273704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com