【问题标题】:How to insert radio button value into the database?如何将单选按钮值插入数据库?
【发布时间】:2015-06-18 14:10:32
【问题描述】:

我想将选定的单选按钮值插入数据库。只是“是”或“否”。如果有人能给我一个想法,那将是一个很大的帮助。我正在使用 codeigniter 框架。提前致谢。

型号

function add_reservation($reservation_model) {
        return $this->db->insert('reservations', $reservation_model);
    }

控制器

function add_reservation() {
        $reservation_model = new Reservation_model();
        $reservation_service = new Reservation_service();

        $reservation_model->set_date_cal(trim($this->input->post('date', TRUE)));
        $reservation_model->set_date(strtotime($this->input->post('date', TRUE)));
        $reservation_model->set_title(trim($this->input->post('selected_hall', TRUE)));
        $reservation_model->set_type(trim($this->input->post('selected_time', TRUE)));
        $reservation_model->set_description(trim($this->input->post('name', TRUE)));
        $reservation_model->set_advanced_payment_status(trim($this->input->post('advanned_paid_radio_button', TRUE)));
        $reservation_model->set_paid_amount(trim($this->input->post('paid_amount', TRUE)));
        $reservation_model->set_fax(trim($this->input->post('fax', TRUE)));
        $reservation_model->set_telephone_number(trim($this->input->post('telephone', TRUE)));
        $reservation_model->set_address(trim($this->input->post('address', TRUE)));
        $reservation_model->set_menu_no(trim($this->input->post('selected_menu_number', TRUE)));
        $reservation_model->set_menu_price_per_plate(trim($this->input->post('menu_price', TRUE)));
        $reservation_model->set_is_deleted('0');

        $this->db->last_query();

        echo $reservation_service->add_reservation($reservation_model);
    }

查看

<label for="advanced_paid">Advanced paid</label>                        
                        <div id="advanned_paid_radio_button">                           
                            <input type="radio" onclick="myFunction()" name="optionsRadios" id="yes" value="yes"> Yes
                            <input type="radio" onclick="myNoFunction()" name="optionsRadios" id="no" value="no"> No

                        </div>

在 javascript 我有这个。

                 function myFunction() {                               

                       if ($('input[id="yes').is(':checked')) {
                            $('div[id="paid"]').show();
                        }
                        if ($('input[id="no"]').is(':checked')) {
                            $('div[id="paid"]').hide();
                        }

                    }

                    function myNoFunction() {
                        if ($('input[id="no"]').is(':checked')) {
                            $('div[id="paid"]').hide();
                        }
                    } 

【问题讨论】:

  • 你现在面临的问题是什么?
  • @SulthanAllaudeen 当我将数据插入到数据库中时,单选按钮值不可用。我想发送单击的单选按钮值。就像“是”或“否”

标签: javascript php codeigniter radio-button


【解决方案1】:

我认为问题在于,您发布的单选按钮 advanned_paid_radio_buttonid 是错误的,您需要在 controller 中发布单选按钮的 name='optionsRadios' 而不是 id advanned_paid_radio_button

$reservation_model->set_advanced_payment_status(trim($this->input->post('optionsRadios', TRUE)));

【讨论】:

    【解决方案2】:

    要确定单选按钮的值,您应通过.val() 获取它

    并将其存储在变量中

    if ($('input[id="yes').is(':checked')) {
    $('div[id="paid"]').show();
    var value = $(this).val();
    //your ajax call shall be here
    }
    
    if ($('input[id="no"]').is(':checked')) {
    $('div[id="paid"]').hide();
    var value = $(this).val();
    //your ajax call shall be here 
    }
    

    然后要在数据库中更新它,您应该对控制器进行 jquery-ajax 调用并在数据库中插入/更新值。

    注意:如果您只想更新数据库中的YesNo,您甚至不需要函数myFunctionmyNoFunction

    您可以在更改事件中使用它

    【讨论】:

    • 我将 myFunction 和 myNoFunction 用于其他目的。如果单选按钮是,我想显示一个文本字段,如果不是,我想隐藏它。为此我只使用了这些方法。
    • 现在我想将 yes 或 no 值发送到 anthor 字段
    • 好的,那么您将通过var value = $(this).val(); 获得结果
    • 您想在其他地方显示YesNo 而不是01
    • 是的。我稍后将其用于搜索目的。无论如何,我会按照你说的方式尝试。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2014-11-04
    相关资源
    最近更新 更多