Php 如何在Codeigniter中创建递增按钮来更新数据库onclick?
我是codeigniter的新手。我有一个项目,管理员分配余额给其经销商。现在,我想在html中创建一个按钮,它为管理员提供了一个界面,可以在单击按钮时增加余额。当他增加它时,数据库中的值也应该更新。也许这很简单,但我做不到!我想我应该使用ajax或Jquery来实现这一点 下面是我的查看代码:想在平衡输入之外添加递增和递减按钮吗Php 如何在Codeigniter中创建递增按钮来更新数据库onclick?,php,html,codeigniter,Php,Html,Codeigniter,我是codeigniter的新手。我有一个项目,管理员分配余额给其经销商。现在,我想在html中创建一个按钮,它为管理员提供了一个界面,可以在单击按钮时增加余额。当他增加它时,数据库中的值也应该更新。也许这很简单,但我做不到!我想我应该使用ajax或Jquery来实现这一点 下面是我的查看代码:想在平衡输入之外添加递增和递减按钮吗 <?php echo validation_errors(); ?> <?php echo form_open(); ?> <table
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>SIP Username</td>
<td><?php echo form_input('sip_username', set_value('sip_username', $user->sip_username)); ?></td>
</tr>
<tr>
<td>SIP Password</td>
<td><?php echo form_input('sip_password', set_value('sip_password', $user->sip_password)); ?></td>
</tr>
<tr>
<td>Key</td>
<td><?php echo form_input('key', set_value('key', $user->key), 'readonly'); ?></td>
</tr>
<tr>
<td>Allocation Block</td>
<td><?php echo form_input('allocation_block', set_value('allocation_block', $user->allocation_block)); ?></td>
</tr>
<tr>
<td>Name</td>
<td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
</tr>
<tr>
<td>Reseller Email</td>
<td><?php echo form_input('email', set_value('email', $user->email)); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo form_password('password'); ?></td>
</tr>
<tr>
<td>Confirm password</td>
<td><?php echo form_password('password_confirm'); ?></td>
</tr>
<tr>
<td>User_Required</td>
<td><?php echo form_input('user_num', set_value('user_num', $user->user_num)); ?></td>
</tr>
<tr>
<td>Balance</td>
<td><?php echo form_input('balance', set_value('balance', $user->balance)); ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td>
</tr>
<tr>
<td>Address</td>
<td><?php echo form_input('address', set_value('address', $user->address)); ?></td>
</tr>
<tr>
<td>Status</td>
<td><?php echo form_dropdown('status', array('Active' => 'Active', 'Inactive' => 'inactive', 'Delete' => 'delete'), $this->input->post('status') ? $this->input->post('status') : $user->status ); ?></td>
</tr>
<tr>
<td>Country</td>
<td><?php echo form_input('country', set_value('country', $user->country)); ?></td>
</tr>
<tr>
<td>Country Code</td>
<td><?php echo form_input('country_code', set_value('country_code', $user->country_code)); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close();?>
控制员:
public function edit ($id = NULL)
{
// Fetch a user or set a new one
if ($id) {
$this->data['user'] = $this->reseller_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else {
$this->data['user'] = $this->reseller_m->get_new();
}
// Set up the form
$rules = $this->reseller_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance','user_num','address','country','country_code','created','modified','status'));
$data['password'] = $this->reseller_m->hash($data['password']);
$key=$this->reseller_m->save($data, $id);
//here we get the last inserted record id in $last_id
$last_id = $this->db->insert_id();
//The logic to create blank rows in user table mapped to reseller_id
$values=array($this->input->post('name'),$this->input->post('country_code'),$this->input->post('allocation_block'),$this->input->post('user_num'));
$key=implode('-',$values);
$this->db->where('id',$last_id);
$this->db->update('reseller',array('key'=>$key));
for($i=1; $i<=$data['user_num'];$i++)
{
$userdata=array('key'=>$key);
// here users is taken name of user table with retailer_id is field
$this->db->insert('users',$userdata);
}
redirect('admin/reseller');
}
// Load the view
$this->data['subview'] = 'admin/reseller/edit';
$this->load->view('admin/_layout_main', $this->data);
}
如果您熟悉ajax,可以使用ajax来实现这一点
单击该按钮,您可以调用ajax并在控制器函数中编写代码以更新数据库中的值。如果您熟悉ajax,可以使用ajax进行此操作
单击按钮,您可以调用ajax并在控制器函数中编写代码以更新数据库中的值。是的,但我不知道如何执行此ajax操作您可以这样做。将id赋予按钮作为更新,然后在加载页脚后添加以下代码$document.onclick,update,function{url:,data:{name:value,name:value},//将所需的值传递给控制器function-type:POST,dataType:HTML,success:function{//成功后,执行此处所需的sutff};我可以使用类似于:function myFunction{document.getElementByIdmyNumber.steppup;}Number:我在W3schools上找到了这个。是的,但是我不知道如何执行这个ajax操作。你可以这样做。将id赋予按钮作为更新,然后在加载页脚后添加以下代码$document.onclick,update,function{url:,data:{name:value,name:value},//将所需的值传递给控制器function-type:POST,dataType:HTML,success:function{//成功后,执行此处所需的sutff};我可以使用类似于:function myFunction{document.getElementByIdmyNumber.steppup;}Number的东西吗:我在W3S上找到了这个。