1

Hello i want to deduct balance of Resellers. Each reseller has its own users so he can edit his users. now if reseller sets balance of user = yes then i want to deduct 5 euros from his account. So how do i know if the radio button value if changed from No to yes?

<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>

Output

So now after login if a reseller sets the balance of user to yes then 5 euros should be deducted! Please help me with this:

The User Controller :

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

}

I have Tried This:

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

BEfore Edit

After Edit

17
  • set hidden field for radio button nd change that hidden value when value change . by hidden button value you will know value is change or not Commented Sep 1, 2015 at 12:07
  • 1
    You surely want to do that using PHP only? because you can no work with DOM using PHP. You need Javascript for that. Commented Sep 1, 2015 at 12:21
  • @Affan can you write down a sample code please? Commented Sep 1, 2015 at 12:22
  • @AnandGhaywankar yes i think i will need javascript but m not sure how to do that. Commented Sep 1, 2015 at 12:23
  • Something like if ($_POST['balance'] == "Yes")? Post the DOM output and the network output. Commented Sep 1, 2015 at 12:33

2 Answers 2

2

You can get the value of the radiobutton by using following code:

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

Now you can do a simple check in your backend:

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

When you want to get the value of a radiobutton and the names are the same(when you have multiple radiobuttons) you can get the value by using $_POST['name of radiobutton']

Example

// HTML

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

// PHP After submit(method post)

$this->input->post('example'); // will be 1 or 2
Sign up to request clarification or add additional context in comments.

1 Comment

one more thing i have many reseller many users. so will this update all users or i can do something which updates only those reseller whose user is edited!
0

you can check the selected radio button in this way:

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

2 Comments

logically i find you answer correct but didnt worked with me in PHP Codeiginter please check the updated question where i have added my User controller code
$_POST['yes'] will be undefined and $selected_radio value will never be checked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.