Welcome Guest, Not a member yet? Register   Sign In
checkbox - Undefined index
#1

[eluser]BaRzO[/eluser]
Hi all. ( First of all i want to thank you for this lovely work )
I have a problem

my view file

Code:
<tr>
<td style='white-space:nowrap'>Pozisyon</td>
<td>
<label for='T'> Üst Menu &lt;input id='T' type='checkbox' name='link_position_t' value='1'&lt;?=$contents-&gt;link_position_t != "0" ? " checked" : ""?&gt;></label>
<label for='L'> Sol Menu &lt;input id='L' type='checkbox' name='link_position_l' value='1'&lt;?=$contents-&gt;link_position_l != "0" ? " checked" : ""?&gt;></label>
<label for='F'> Alt Menu &lt;input id='F' type='checkbox' name='link_position_f' value='1'&lt;?=$contents-&gt;link_position_f != "0" ? " checked" : ""?&gt;></label>
</td>
</tr>

and my controller


Code:
$this->link_position_f = $_POST['link_position_f'];

die($this->link_position_f);

when i post the form unchecked value of checkbox form givin error as you can see it at bellow
what is my wrong Sad

error
Severity: Notice
Message: Undefined index: link_position_f

thanks for help...
#2

[eluser]xwero[/eluser]
Don't you get a blank page if the link_position_f isn't checked because of the die function?

I think you do the check better in the controller than in the view
Code:
// controller
$data['link_position_t'] = (isset($_POST['link_position_f']))?" checked":"";
$this->load->view('view',$data);
// view
<label for='F'> Alt Menu &lt;input id='F' type='checkbox' name='link_position_f' value='1'&lt;?=$link_position_f; ?&gt;&gt;</label>
#3

[eluser]BaRzO[/eluser]
i am having problem when a user submit form with empty checkboxs there is no problem if checkbox checked...

ps. i am testing with die
#4

[eluser]xwero[/eluser]
A checkbox is different from an input in the way that if the checkbox isn't checked the $_POST key doesn't exist. That is why i used the isset function to see if the key is in the $_POST global. As an alternative you could use array_key_exists.

You could do the same check in the view but i don't see why you should create a object property to hold the value for that particular key? ($this->link_position_f = $_POST['link_position_f']Wink
#5

[eluser]BaRzO[/eluser]
Thanks for your help

i am not good as you can see this is my update function and its now working...

pls tell if its worng thanks for you help Smile

Code:
function update() {

        $this->id = $_POST['id'];
        $this->link_title = $_POST['link_title'];
        $this->link_route = $this->seo->seoname($_POST['link_title']);
        $this->link_blank = $_POST['link_blank'];
        $this->link_order = $_POST['link_order'];

        if ( array_key_exists('link_position_t', $_POST) ) { $this->link_position_t = '1'; } else { $this->link_position_t = '0'; }
        if ( array_key_exists('link_position_l', $_POST) ) { $this->link_position_l = '1'; } else { $this->link_position_l = '0'; }
        if ( array_key_exists('link_position_f', $_POST) ) { $this->link_position_f = '1'; } else { $this->link_position_f = '0'; }

        $this->content = $this->seo->p2br($_POST['content']);
        $this->author = '1'; // kullanici ekle...
        $this->active = $_POST['active'];
        $this->time = time();

        // update...
        $this->db->where('id',$this->id);
    if ( $this->db->update('content',$this) ) {
            // ok
            $this->template['result'] = 'Result... Ok...';
        } else {
            // false
            $this->template['result'] = 'Result... Not... Ok...';
        }

        $this->_run('settings_update_view');
    }
#6

[eluser]xwero[/eluser]
What is the error you get?
#7

[eluser]BaRzO[/eluser]
No error at all i just want show you how am i coding thats it...
after your help there is no more error...

Thanks a lot...
#8

[eluser]Chris Newton[/eluser]
You could also use:

Code:
$this->link_position_t=$this->input->post('link_position_t');

instead of:

if ( array_key_exists('link_position_t', $_POST) ) { $this->link_position_t = '1'; } else { $this->link_position_t = '0'; }
$this->input->post('variablename') returns FALSE if a variable is not set, or the value if it is.

Also, I would suggest making a new object for your update data rather than using $this so you don't inadvertently send variables you don't need to. You could easily use something like $data or $content
#9

[eluser]BaRzO[/eluser]
Many thanks for your helps...
#10

[eluser]Stoney[/eluser]
Or you can do it like this:

Code:
$formdata = $_POST;
if ( !$this->input->post('your_checkbox_field')) $formdata['your_checkbox_field'] = '0';
$this->db->where('id', $this->input->post('id'));
$this->db->update('your_table',$formdata);




Theme © iAndrew 2016 - Forum software by © MyBB