Question

I am having some trouble setting keeping RADIO BUTTON checked.

Normally without CI I did it:

    <input  name="type" value="1" checked="checked"  type="radio" onChange="this.form.submit();"/>

    Offer

    <?php 
   if( !empty($_SESSION['type']) && $_SESSION['type'] == 2 )

       {

           echo "<input type=\"radio\" name=\"type\" value=\"2\"

           checked = \"checked\" onChange=\"this.form.submit();\" />";

           }else{

           echo "<input type=\"radio\" name=\"type\" value=\"2\" 

           onChange\"this.form.submit();\" 

           />";
   }     
    ?>
   Search

And then I use a condition:

    if(!empty($_SESSION['type']) && $_SESSION['type'] == 1)

    {

     dosomthing()

    }

I gave it a try, and I am getting two problems.

1- I'm using Onchange method to refresh the page, but it actually sending the form. (This didn't happen when I wasn't using CI)

2- When I select Search radio it doesn't stay there, it goes back to first radio.

This is what I did.

      <?php echo form_radio('type', '1', TRUE, 'onChange="this.form.submit();"'); ?>

      Offer

      <?php

      if( $this->session->userdata('type') && $this->session->userdata('type') == 2 )

       {

        echo form_radio('type', '2', TRUE, 'onChange="this.form.submit();"');

       }else{

        echo form_radio('type', '2', FALSE, 'onChange="this.form.submit();"');

       }

       ?>

       Search

      <?php

      if(($this->session->userdata('type')) && $this->session->userdata('type') == 1) 

       {

       dosomething();

       }
      ?>
Était-ce utile?

La solution

Try this, You may need to change session value in your controller or model like this:

if($this->input->post("type") && $this->input->post("type")==2){
 $this->session->set_userdata('type', 2);
}
else{
 $this->session->set_userdata('type', 1);
}

Inside View:

<?php

      if( $this->session->userdata('type') && $this->session->userdata('type') == 2 )

       {

        echo form_radio('type', '2', TRUE, 'onchange="this.form.submit();"');

       }else{

        echo form_radio('type', '2', FALSE, 'onchange="this.form.submit();"');

       }

       ?>

       Search

      <?php

      if(($this->session->userdata('type')) && $this->session->userdata('type') == 1) 

       {

       dosomething();

       }
      ?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top