如何检查PHP Codeiginter中是否更改了单选按钮的值?

时间:2015-09-01 11:05:29

标签: php codeigniter

您好我想扣除经销商的余额。每个经销商都有自己的用户,因此他可以编辑他的用户。现在,如果经销商设置用户余额=是,那么我想从他的帐户中扣除5欧元。那么我如何知道单选按钮值是否从No更改为yes?

Kerberos

Output

所以现在登录后,如果经销商将用户余额设置为是,则应扣除5欧元!请帮帮我:

用户控制器:

<tr>
    <td>Balance</td>
<td>Yes<?php echo form_radio('balance','Yes', $this->input->post('balance') ? $this->input->post('balance') : $user->balance);  ?> No <?php echo form_radio('balance','No', $this->input->post('balance') ? $this->input->post('balance') : $user->balance);  ?></td>   
</tr>

我试过这个:

public function edit ($id = NULL)
{

     $usertype=$this->session->userdata('usertype');
    if($usertype ==="reseller")
    {

    // Fetch a user or set a new one
    if ($id) {
        $this->data['user'] = $this->user_m->get($id);
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
    }
    else {
        $this->data['user'] = $this->user_m->get_new();
    }

    // Set up the form
    $rules = $this->user_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->user_m->array_from_post(array('sip_id','sip_pass','name','email', 'password','phone','status','created','balance'));
        $data['password'] = $this->user_m->hash($data['password']);

            $selected_radio=$_POST['yes']; 
                                    if($selected_radio=='checked')
                                    { 
                                        $this->reseller_m->deduct();
                                    } 



        $this->user_m->save($data, $id);
        redirect('reseller/user');
    }

    // Load the view
    $this->data['subview'] = 'reseller/user/edit';
    $this->load->view('reseller/_layout_main', $this->data);


}
else{
    $this->load->view('permission');
}

}

BEfore Edit

After Edit

2 个答案:

答案 0 :(得分:2)

您可以使用以下代码获取radiobutton的值:

$this->input->post('balance')

现在您可以在后端进行简单的检查:

if ($this->input->post('balance') === 'Yes'){
  //execute when true
} else{
  // execute other action
}

如果要获取单选按钮的值并且名称相同(当您有多个单选按钮时),可以使用$_POST['name of radiobutton']

获取值

实施例

// HTML

<input type="radio" name="example" value="1" />
<input type="radio" name="example" value="2" />

// PHP 提交后(方法发布)

$this->input->post('example'); // will be 1 or 2

答案 1 :(得分:0)

您可以这样检查所选的单选按钮:

<?php 
$selected_radio=$_POST['yes']; 
if($selected_radio=='checked'){ 
//deduct 5 euros;
} 
?>