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

Rajan
  • 2,427
  • 10
  • 51
  • 111
  • 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 – Affan Sep 01 '15 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. – Anand G Sep 01 '15 at 12:21
  • @Affan can you write down a sample code please? – Rajan Sep 01 '15 at 12:22
  • @AnandGhaywankar yes i think i will need javascript but m not sure how to do that. – Rajan Sep 01 '15 at 12:23
  • Something like `if ($_POST['balance'] == "Yes")`? Post the DOM output and the network output. – GuyT Sep 01 '15 at 12:33
  • @GuyT Please check the updated question i have added what i have tried so far. I check it the radio button is checked when user is edited. if the radio button is set to yes then deduction should work but it does'nt – Rajan Sep 01 '15 at 13:50
  • @Rajan Could you post the generated HTML of the radiobutton? – GuyT Sep 01 '15 at 13:51
  • I have already See the first code line. i have added a screen shot and the html code of radio button – Rajan Sep 01 '15 at 13:55
  • @Rajan That's not the generated code, but the PHP code. In your browser hit F12 and use the inspector to inspect the specific DOM element. – GuyT Sep 01 '15 at 13:59
  • @GuyT i have added the screen shot of specifc DOM element – Rajan Sep 01 '15 at 14:04
  • @Rajan Much better! So, do you want to reflect the balance deduction directly to the screen or after a reload? If you want to use the first option you have to use JavaScript otherwise you can do it in PHP. For the PHP solution you could use `$this->input->post('balance')` and check the value. Remember to always santinize the user input. – GuyT Sep 01 '15 at 14:11
  • @GuyT I want to do the deduction after a reload. can you help me with the code please – Rajan Sep 02 '15 at 05:32
  • @GuyT i did'nt understood this : santinize the user input. – Rajan Sep 02 '15 at 05:50
  • @Rajan So could you print `$this->input->post('balance')`? Looking at the DOM element I guess it will work because the name is the same for the 2 radiobuttons so it will give you the values back("Yes" or "No"). With santinizing the input I mean that you always have to check if the user input is the input you expect otherwise your application may be vulnerable to several injections. – GuyT Sep 02 '15 at 06:14
  • @Rajan Look for CodeIgniter pages on Google like: https://ellislab.com/codeigniter/user-guide/libraries/security.html , http://stackoverflow.com/questions/14757812/codeigniter-best-practice-to-sanitize-input , http://stackoverflow.com/questions/2401706/where-to-sanitize-php-post-input – GuyT Sep 02 '15 at 06:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88539/discussion-between-rajan-and-guyt). – Rajan Sep 02 '15 at 06:29
  • Where do i check the condition whether the radio is checked or not? And if checked deduct balance? I am sorry but i didnt understood you. – Rajan Sep 02 '15 at 06:33

2 Answers2

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
GuyT
  • 4,316
  • 2
  • 16
  • 30
  • 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! – Rajan Sep 02 '15 at 10:50
0

you can check the selected radio button in this way:

<?php 
$selected_radio=$_POST['yes']; 
if($selected_radio=='checked'){ 
//deduct 5 euros;
} 
?>
Numair
  • 69
  • 8
  • 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 – Rajan Sep 01 '15 at 12:27
  • `$_POST['yes']` will be undefined and `$selected_radio` value will never be `checked` – GuyT Sep 02 '15 at 10:46