开发者

check box value not saved properly in database

开发者 https://www.devze.com 2023-03-19 20:11 出处:网络
I write this from in Codeigniter as a form_view.php. <?php echo form_open(\'form\'); ?> <h5>Man</h5>

I write this from in Codeigniter as a form_view.php.

<?php echo form_open('form'); ?>

<h5>Man</h5>
<input type="checkbox" name="options[]" value="m"/>

<h5>Lady</h5>
<input type="checkbox" name="options[]" value="f" />

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<div><input type="submit" value="Submit" /></div>

</form>

And then I add this data to the database like this:

  $data=array(
            开发者_开发百科'type'=> $_POST['options[]'],
            'name'=> $_POST['name']
            );
    $this->db->insert('user_data',$data);

The name field is ok. But the type field has nothing. I can not find the error. Please help me.


Assuming that you want multiple values to be stored with comma (as separator) between them

$data=array(
        'type'=> implode(',', $_POST['options']),
        'name'=> $_POST['name']
        );


Remove [] from options.

$_POST['options'] will be an array. You may have to join the values to a string.


A few suggestions:

  1. If neither one is checked the value will not be submitted to the database, so you need a default.
  2. Consider using a radio type instead of checkbox. Except for some rare folks, I don't think too many people are going to be both man and lady.
  3. Change $_POST['options[]'] to $_POST['options']


Make

'type'=> $_POST['options[]']

as

'type'=> $_POST['options']
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号