【问题标题】:how to check if value of radio button is changed in PHP Codeiginter?如何检查 PHP Codeigniter 中单选按钮的值是否已更改?
【发布时间】:2015-09-02 18:58:24
【问题描述】:

您好,我想扣除经销商的余额。每个经销商都有自己的用户,因此他可以编辑他的用户。现在,如果经销商设置用户余额 = 是,那么我想从他的帐户中扣除 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>

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

用户控制器:

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');
}

}

我试过这个:

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

【问题讨论】:

  • 为单选按钮设置隐藏字段并在值更改时更改该隐藏值。通过隐藏按钮的值,您将知道值是否发生变化
  • 您肯定想只使用 PHP 来实现吗?因为你不能使用 PHP 处理 DOM。为此,您需要 Javascript。
  • @Affan 你能写下示例代码吗?
  • @AnandGhaywankar 是的,我想我需要 javascript,但我不知道该怎么做。
  • 类似if ($_POST['balance'] == "Yes")?发布 DOM 输出和网络输出。

标签: php codeigniter


【解决方案1】:

您可以使用以下代码获取单选按钮的值:

$this-&gt;input-&gt;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

【讨论】:

  • 我还有很多经销商很多用户。这将更新所有用户,或者我可以做一些只更新用户被编辑的经销商的事情!
【解决方案2】:

您可以通过这种方式检查选定的单选按钮:

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

【讨论】:

  • 从逻辑上讲,我发现您的回答正确,但在 PHP Codeiginter 中没有与我合作,请检查我添加了用户控制器代码的更新问题
  • $_POST['yes'] 将是未定义的,$selected_radio 的值永远不会是 checked
猜你喜欢
  • 2023-02-03
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 2013-05-22
  • 2014-05-31
  • 1970-01-01
相关资源
最近更新 更多