繁体   English   中英

php-如果选择了单选按钮,请从数据库表中选择值

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

我正在使用codeigniter,单选按钮,javascript和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的值将显示类似“ RSL”的代号,当选择Dropshipper时, 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等。

希望它能帮助需要我做同样事情的其他人:)

请尝试以下操作:

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

我想就是你想要摆弄

<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());
});
if($_POST['customerType'] == "Reseller"){
    $_POST['customerCode'] = "RSL";

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

仅用php的最简单方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM