我遇到了一个问题,我需要在显示的同一页面中更新数据库中的某些信息。
在这种情况下,我从索引页面中的网站获取一些“全局设置”,其中包含一个表单。这是一张图片,只是为了让我更清楚地理解我的意思。
正如你所看到我创建了按钮,我可以从数据库中看到值,问题是我无法弄清楚如何从那里更新它。有人可以提出一个想法吗?
这是我的控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings extends Admin_Controller {
public function index()
{
$this->form_validation->set_rules('website_favicon', 'Favicon', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_logo', 'Logon', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_name', 'Website Name', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_credits', 'Credits', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_credits_link', 'Credits Link', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_copyright', 'Copyright', 'trim|required|min_length[4]');
if($this->form_validation->run() == FALSE){
// Get Current Subject
$data['item'] = $this->Settings_model->get_website_data();
//Load View Into Template
$this->template->load('admin', 'default', 'settings/index', $data);
} else {
// Create website settings
$data = array(
'website_favicon' => $this->input->post('website_favicon'),
'website_logo' => $this->input->post('website_logo'),
'website_name' => $this->input->post('webiste_name'),
'website_credits' => $this->input->post('website_credits'),
'website_credits_link' => $this->input->post('website_credits_link'),
'website_copyright' => $this->input->post('website_copyright'),
);
// Update User
$this->Settings_model->update($id, $data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'website settings',
'action' => 'updated',
'user_id' => $this->session->userdata('user_id'),
'message' => 'User (' . $data["username"] . ') updated the website settings'
);
// Add Activity
$this->Activity_model->add($data);
//Create Message
$this->session->set_flashdata('success', 'Website setting has been updated');
//Redirect to Users
redirect('admin/settings');
}
}
}
这是我的模特:
<?php
class Settings_model extends CI_MODEL
{
function __construct()
{
parent::__construct();
$this->table = 'website_settings';
}
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
public function get_website_data()
{
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('id', 1);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->row();
} else {
return false;
}
}
}
这是我的视图(index.php),格式为:
<h2 class="page-header">Website Settings</h2>
<?php if($this->session->flashdata('success')) : ?>
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i> Alert!</h4>
<?php echo $this->session->flashdata('success') ?></div>
<?php endif; ?>
<?php if($this->session->flashdata('error')) : ?>
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> Alert!</h4>
<?php echo $this->session->flashdata('error') ?></div>
<?php endif; ?>
<?php echo validation_errors('<p class="alert alert-danger">'); ?>
<?php echo form_open('admin/settings/index/'.$item->id); ?>
<!-- Website Favicon -->
<div class="form-group">
<?php echo form_label('Website Favicon', 'title'); ?>
<?php
$data = array(
'name' => 'website_favicon',
'id' => 'website_favicon',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_favicon
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Logo -->
<div class="form-group">
<?php echo form_label('Website Logo', 'website_logo'); ?>
<?php
$data = array(
'name' => 'website_logo',
'id' => 'website_logo',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_logo
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Name -->
<div class="form-group">
<?php echo form_label('Website Name', 'website_name'); ?>
<?php
$data = array(
'name' => 'website_name',
'id' => 'website_name',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_name
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Credits -->
<div class="form-group">
<?php echo form_label('Website Credits to', 'website_credits'); ?>
<?php
$data = array(
'name' => 'website_credits',
'id' => 'website_credits',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_credits
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Credits Link -->
<div class="form-group">
<?php echo form_label('Website Credits to Link', 'website_credits_link'); ?>
<?php
$data = array(
'name' => 'website_credits_link',
'id' => 'website_credits_link',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_credits_link
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Copyright -->
<div class="form-group">
<?php echo form_label('Copyrights', 'website_copyright'); ?>
<?php
$data = array(
'name' => 'website_copyright',
'id' => 'website_copyright',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_copyright
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website First Ad -->
<div class="form-group">
<?php echo form_label('Ad One', 'website_first_ad'); ?>
<?php
$data = array(
'name' => 'website_first_ad',
'id' => 'website_first_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_first_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Website Second Ad -->
<div class="form-group">
<?php echo form_label('Ad Two', 'website_second_ad'); ?>
<?php
$data = array(
'name' => 'website_second_ad',
'id' => 'website_second_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_second_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Website Third Ad -->
<div class="form-group">
<?php echo form_label('Ad Three', 'website_third_ad'); ?>
<?php
$data = array(
'name' => 'website_third_ad',
'id' => 'website_third_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_third_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<?php echo form_submit('mysubmit', 'Update Website', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
感谢您的帮助。
答案 0 :(得分:0)
单击该按钮,您可以绑定ajax调用以将数据提交到控制器的更新操作,您可以处理响应以在同一页面上显示相关消息。
示例ajax调用
$.ajax({
url:'settings/update',//controller action
type:'POST',
dataType:'JSON',
data:{'data':data,'id':id},//form data you need to upate with the id
success:function(response) {
//show success message here
},
error:function(response) {
//show error message here
}
});
希望这有帮助。
答案 1 :(得分:0)
检查数据是否通过控制器发布。然后使用set_value到输入字段以保留提交后的值
<强> CONTROLLER 强>
var christmasTicket = db.ChristmasTicketsDb.FirstOrDefault(x => x.BarcodeNum == BarcodeNum);
if (christmasTicket != null)
{
// found !
// here you can take the id from christmasTicket
}
查看强>
.p12