2015-06-09 93 views
0

我正在使用codeigniter,我有單選按鈕,JavaScript和PHP的問題。PHP - 如果單選按鈕選擇,從數據庫表中選擇值

單選按鈕用於選擇客戶類型。這是上查看文件我的單選按鈕

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

經銷商單選按鈕選擇,我想customerCode的值將顯示的dropshipper選擇的代號一樣「RSL」,當,值customerCode自動顯示「DRP」

請指點,
非常感謝你:)

============================後問題============================
嗨,大家好,這個案例解決後,我想分享我的代碼。
我們知道,這個線程會自動生成表單輸入的值,取決於單選按鈕。我們懂了!感謝Ashwani Goyal :)
現在,我想給這個表單輸入一個數字字符。所以這是我的模型代碼:

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

這是我的控制器:

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

因此,輸出將是這樣的:
如果選擇DRP然後輸出; DRP001,DRP002,...以及更多
如果RSL選擇,則輸出是; RSL001,RSL002,...以及更多。

希望它會幫助別人那裏誰需要同樣的事情我:)

回答

1

嘗試以下操作:

$("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" />

+0

老兄,你是石頭!感謝你幫助像我這樣的新手:) 我將結合這種方法與我自己的代碼模型來自動生成代碼..謝謝你:) – user2236102

+0

快樂幫助你。 –

0

我認爲是你想要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()); 
}); 
0
if($_POST['customerType'] == "Reseller"){ 
    $_POST['customerCode'] = "RSL"; 

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

最簡單的方法只有php