简体   繁体   English

如何将单选按钮值插入数据库?

[英]How to insert radio button value into the database?

I want to insert selected radio button value into the database.我想将选定的单选按钮值插入到数据库中。 just "yes" or "no".只是“是”或“否”。 if anyone can give me an idea it would be a great help.如果有人能给我一个想法,那将是一个很大的帮助。 I am using codeigniter framework.我正在使用 codeigniter 框架。 Thanks in advance.提前致谢。

Model模型

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

Controller控制器

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

View看法

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

In the javascript i have this.在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();
                        }
                    } 

To determine the value of the radio button then you shall get it by .val()要确定单选按钮的值,则应通过.val()获取它

And store it in a variable并将其存储在变量中

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 
}

Then to update it in the database, You shall have a jquery-ajax call to the controller and insert/update the value in your database.然后要在数据库中更新它,您应该对控制器进行 jquery-ajax 调用并插入/更新数据库中的值。

Note : If you just want to update it the Yes or No in the Database you don't even need to have functions myFunction or myNoFunction .注意:如果您只想更新数据库中的YesNo ,您甚至不需要函数myFunctionmyNoFunction

You can just have it in it's on change event你可以把它放在它的变化事件中

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

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

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

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