user2236102
user2236102

Reputation: 77

php - if radio button selected, select value from table of database

I'm using codeigniter and i have some issue with radio button, javascript, and php.

The radio button is used for selecting type of customer. This is my radio button on view file

<input type="radio" name="customerType" value="Reseller" /> Reseller
<input type="radio" name="customerType" value="Dropshipper" /> Dropshipper
<input type="text" class="form-control" value ="" name="customerCode" aria-describedby="basic-addon1" />

When Reseller radio button selected, I want the value of customerCode will showing codename like "RSL", and when Dropshipper selected, the value of customerCode showing "DRP" automatically.

Please advice,
Thank you very much :)

============================AFTER SOLVED============================
Hi guys, after this case was solved i want to share my code.
as we know, this thread will auto generating value of form input depends by radio button. And we got it! Thanks to Ashwani Goyal :)
And now, I want to give a numerical character on this form input. So this is my model code :

    function getCustomerCodeRSL(){ //this function will generating when "RSL" radio button selected
    $q = $this->db->query("select MAX(RIGHT(customer_code,3)) as codeMax from customer where customer_code like 'RSL%'");
    $code = "";
    if($q->num_rows()>0){
        foreach($q->result() as $k){
            $tmp = ((int)$k->codeMax)+1;
            $code = sprintf("%03s", $tmp);
        }
    }else{
        $code = "001";
    }
    return $code;
}

function getCustomerCodeDRP(){ //this function will generating when "DRP" radio button selected
    $q = $this->db->query("select MAX(RIGHT(customer_code,3)) as codeMax from customer where customer_code like 'DRP%'");
    $code = "";
    if($q->num_rows()>0){
        foreach($q->result() as $k){
            $tmp = ((int)$k->codeMax)+1;
            $code = sprintf("%03s", $tmp);
        }
    }else{
        $code = "001";
    }
    return $code;
}

And this is my controller :

    function cCustomer(){
    $data = array(
        'data_customer'     => $this->Model_App->getAllData('customer'),
        'total_customer'    => $this->Model_App->counterAllRowTable('customer'),
        'cust_codeRSL'      => $this->Model_App->getCustomerCodeRSL(),
        'cust_codeDRP'      => $this->Model_App->getCustomerCodeDRP(),
    );
    $this->load->view('admin/header');
    $this->load->view('admin/vCustomer', $data);
    $this->load->view('admin/footer');
}

So, the output will be like this :
if DRP selected then the output is; DRP001, DRP002, ... and more
if RSL selected then the output is; RSL001, RSL002, ... and more.

hope it will helping someone else out there who need the same thing with me :)

Upvotes: 0

Views: 594

Answers (3)

Ashwani Goyal
Ashwani Goyal

Reputation: 616

Try the following:

$("input[name='customerType']").click(function(){
   $("input[name='customerCode']").val( $(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="customerType" value="RSL" id="rsl" /><label for="rsl">Reseller</label>
<input type="radio" name="customerType" value="DSL" id="dsl"/> <label for="dsl">Dropshipper</label>
<input type="text" class="form-control" value ="" name="customerCode" aria-describedby="basic-addon1" />

Upvotes: 1

thomashoareau
thomashoareau

Reputation: 99

if($_POST['customerType'] == "Reseller"){
    $_POST['customerCode'] = "RSL";

}elseif($_POST['customerType'] == "Dropshipper"){
    $_POST['customerCode'] = "DRP";
}

Easiest way with only php

Upvotes: 0

Aabid
Aabid

Reputation: 941

i think thats it you want fiddle

<input type="radio" name="customerType" value="RSL" /> Reseller
<input type="radio" name="customerType" value="DSL" /> Dropshipper
<input type="text" class="form-control" value ="" name="customerCode" aria-describedby="basic-addon1" /

$("input[name='customerType']").click(function(){
   $("input[name='customerCode']").val( $(this).val());
});

Upvotes: 0

Related Questions