CodeIgniter Forums
Radio Button Problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Radio Button Problem (/showthread.php?tid=51548)



Radio Button Problem - El Forum - 05-08-2012

[eluser]taterman[/eluser]
I need a little help please. After my form is submitted, I cannot get my radio buttons to return a value

Code:
<input type="radio" name="years" value="1" <?php echo set_radio('years', '1', TRUE); ?> />
<strong class="label">1 Year</strong>
&lt;input type="radio" name="years" value="2" &lt;?php echo set_radio('years', '2'); ?&gt; /&gt;
<strong class="label">2 Years</strong>
&lt;input type="radio" name="years" value="3" &lt;?php echo set_radio('years', '3'); ?&gt; /&gt;
<strong class="label">3 Years</strong>

and

Code:
$order = array(
'customer' => $result->customer,
'order_date'  => $result->order_date,
'po_number' => $result->po_number,
'order_type' => $result->order_type,
'product' => $product,
'serial' => $this->input->post('serial'),
'ship_id' => $result->ship_id,
'years'  => $this->input->post('years'),
'cost'  => $this->get_maintenance_cost($years, $product)
);

var_dump($order);

array
  'customer' => string 'ASAP BOOKKEEPING' (length=16)
  'order_date' => string '2012-05-08' (length=10)
  'po_number' => string 'Email 2012-05-08' (length=16)
  'order_type' => string 'SOLD' (length=4)
  'product' => string 'OptiQroute 2120 R SW US' (length=23)
  'serial' => string '080900037' (length=9)
  'ship_id' => string '2012A00001' (length=10)
  'years' => boolean false
  'cost' => int 0


I do not understand why 'years' is returning Boolean and not the chosen value


Radio Button Problem - El Forum - 05-08-2012

[eluser]InsiteFX[/eluser]
The form radio buttons and checkboxes do not return a value unless they are checked!



Radio Button Problem - El Forum - 05-08-2012

[eluser]taterman[/eluser]
Thanks for responding. I believe I have the radio buttons returning a default value of "1", but it's not working. 


Radio Button Problem - El Forum - 05-08-2012

[eluser]weboap[/eluser]
can you past the full form, i copy/past your radio piece of the form in a view and a quick function in a controller and it works.
view :
&lt;?php
echo form_open('go');
?&gt;

&lt;input type="radio" name="years" value="1" &lt;?php echo set_radio('years', 1, TRUE); ?&gt; /&gt;
<strong class="label">1 Year</strong>
&lt;input type="radio" name="years" value="2" &lt;?php echo set_radio('years', 2); ?&gt; /&gt;
<strong class="label">2 Years</strong>
&lt;input type="radio" name="years" value="3" &lt;?php echo set_radio('years', 3); ?&gt; /&gt;
<strong class="label">3 Years</strong>

&lt;?php
echo form_submit('go', 'Submit!');
echo form_close();
?&gt;


controller

public function go()
{
$years = $this->input->post('years');
var_dump($years);

}

its returning a string thought, do you have a years var as an integer or array.... looks like php is telling you bad var type.

hope it help.






Radio Button Problem - El Forum - 05-09-2012

[eluser]Ayeyermaw[/eluser]
I think the problem here is that your radio button group needs to be renamed slightly.

Instead of
Code:
name="years"
try
Code:
name="years[]"


It states in the Form Validation section of the user manual that:
"If you use an array as the name of a form field, you must supply it as an array to the function."

Hope that helps


Radio Button Problem - El Forum - 05-09-2012

[eluser]taterman[/eluser]
Here is my full view file. It uses Ajax on submit to validate input fields in the Controller. If inputs are fine then it passes control to the Model.

Thanks for the help everyone.

Code:
&lt;?php

//create input field data
$client = array(
  'name' => 'customer',
  'id'  => 'customer',
  'value' => $this->session->userdata('customer')
);

//create input field data
$po_number = array(
  'name' => 'po_number',
  'id'  => 'po_number',
  'value' => $this->session->userdata('po_number')
);

//create input field data
$ship_number = array(
  'name' => 'ship_number',
  'id'  => 'ship_number',
  'value' => $this->session->userdata('ship_id')
);

$readonly = 'readonly';
$id_1 = 'id="product"';
$id_2 = 'id="serial"';

$units  = $this->session->userdata('units');
$unit_count = $this->session->userdata('unit_count');

?&gt;

[removed][removed]
[removed][removed]

<div id="units_to_ship">
<h1>Add Products</h1>
<fieldset id="fieldset">
  <legend class="legend">&lt;?php echo "{$unit_count} of {$units} Products Added"; ?&gt;</legend>
  &lt;?php
  echo form_open('')."\n"; //controller/method
  ?&gt;
  <div id="add_to_order">
   <div id="order_details">
    <div id="order_details_left">
     <label class="label" for="customer">Customer: </label>&lt;?php echo form_input($client, '', $readonly)."\n"; ?&gt;
    </div>
    <div id="order_details_middle">
     <label class="label" for="po_number">PO Number: </label>&lt;?php echo form_input($po_number, '', $readonly)."\n"; ?&gt;
    </div>
    <div id="order_details_right">
     <label class="label" for="ship_number">Shipping Number: </label>&lt;?php echo form_input($ship_number, '', $readonly)."\n"; ?&gt;
    </div>
   </div>&lt;!--------- end order_details -----&gt;
   <br />  
  
   <label class="label" for="product">Product: </label>&lt;?php echo form_dropdown('product', $products, '', $id_1)."\n"; ?&gt;<br />
  
   [removed]
    $("#product").change(function() {
    
     var id = $('#product').val();
    
     $('#serial').empty();
    
     if(id != ''){
          
      $.post("units_to_ship/display_serials", {'id' : id},
       function(data){
        $('#serial').append(data.result);
       }, "json"
      );
      
      $("#show_serial").show("fast");
      
     }else{
      
      $("#show_serial").hide("fast");
      $('#serial').val('');
      
     }
    
    });  
   [removed]
  
   <div id="show_serial">
    <label class="label" for="serial">Serial: </label><select name="serial" id="serial"></select><br />
   </div>
  
   &lt;?php
    if(stristr($ship_number['value'], 'A') == TRUE){
   ?&gt;
     <label class="label" for="years">Maintenance: </label>
     &lt;input type = "radio" name = "years" value = "1" &lt;?php echo set_radio('years', '1', TRUE); ?&gt; /&gt;
     <strong class="label">1 Year</strong>
     &lt;input type = "radio" name = "years" value = "2" &lt;?php echo set_radio('years', '2'); ?&gt; /&gt;
     <strong class="label">2 Years</strong>
     &lt;input type = "radio" name = "years" value = "3" &lt;?php echo set_radio('years', '3'); ?&gt; /&gt;
     <strong class="label">3 Years</strong>
   &lt;?php
    }
   ?&gt;
    
  </div>&lt;!--------- end add_to_order -----&gt;
  &lt;?php
  echo form_submit('submit', 'Submit', 'id="submit"');
  ?&gt;
  &lt;!--------- form validation on submit  -----&gt;
  [removed]
   $(document).ready(function() {
    $('#submit').click(function() {
    
     var form_data = {
      product : $('#product').val(),
      serial : $('#serial').val()
     };
    
     $.ajax({
      url: "units_to_ship/ajax_check",
      type: 'POST',
      async : false,
      data: form_data,
      success: function(msg) {
       if(msg != ''){
        $('#errors').html(msg);
       }else{
        $("body").load("&lt;?php current_url() ?&gt;");
       }
      }
     });

     return false;
    });
   });
  [removed]    
  
</fieldset>
<div id="errors" class="errors"></div>
</div>



Radio Button Problem - El Forum - 05-09-2012

[eluser]weboap[/eluser]
"
var form_data = {
product : $('#product').val(),
serial : $('#serial').val()
};

"


you are not sending years var to the controller??? if yes where is the piece of code you are using?


Radio Button Problem - El Forum - 05-09-2012

[eluser]taterman[/eluser]
AH!! Good catch, weboap! Thank you! Sometimes it's the little things that are missed.


Radio Button Problem - El Forum - 05-09-2012

[eluser]taterman[/eluser]
I'm sure that was part of the problem, but the radio button is still returning
Code:
'years' => boolean false
.


Radio Button Problem - El Forum - 05-09-2012

[eluser]taterman[/eluser]
It's working now. I forgot to add the id tags. Thanks again, weboap and everyone else who posted here.