CodeIgniter Forums
Radio button - 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 (/showthread.php?tid=55679)

Pages: 1 2


Radio button - El Forum - 11-06-2012

[eluser]hujan[/eluser]
hi all,

I'm new in CodeIgniter and I need help on my radio button..the problem is it did not recorded/inserted into the database. below are my code on the radio button area.

at the view script:

Code:
<p>
        
&lt;input type="radio" name="sap_radio1" value="sap_yes" &lt;?php echo set_radio('sap_radio', '1'); ?&gt; /&gt;        
<label for="sap_yes">Yes</label>
&lt;input type="radio" name="sap_radio1" value="sap_no" &lt;?php echo set_radio('sap_radio', '1'); ?&gt; /&gt;
<label for="sap_no">No</label>
</p>

at my model:

Code:
$data = array(  'sap_yes'=>$this->input->post('sap_yes'),
         'sap_no'=>$this->input->post('sap_no'));


please help me..being stuck at this part for the whole day already.


Radio button - El Forum - 11-06-2012

[eluser]Rabbel[/eluser]
He hujan, shouldn't you use the name selectors instead of the values?

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

And if you still have problems, you should try to 'debug' your code a bit. Like putting echo's everywhere. You can try this to see what your code does after submitting values:

Code:
echo $this->input->post('sap_radio1');



Radio button - El Forum - 11-06-2012

[eluser]noslen1[/eluser]
You have to fill with field name inside $this->input->post(), not value.
The method $this->input->post() will return to you the value.

So you should write...

Code:
$data = array('sap'=>$this->input->post('sap_radio1'))

... and it will store $data['sap'] = "sap_yes" or $data['sap'] = "sap_no" according to what radio button has been clicked.


Radio button - El Forum - 11-06-2012

[eluser]hujan[/eluser]
this is in the view part

Code:
<p>
&lt;input type="radio" name="sap_radio1" value="sap_yes" &lt;?php echo set_radio('sap_radio', '1'); ?&gt; /&gt;
&lt;label for="sap_yes">Yes</label>
        
&lt;input type="radio" name="sap_radio1" value="sap_no" &lt;?php echo set_radio('sap_radio1', '2'); ?&gt; /&gt;
&lt;label for="sap_no">No</label>
</p>

<p>
&lt;input type="radio" name="sap_radio2" value="sponser" &lt;?php echo set_radio('sap_radio2', '1'); ?&gt; /&gt;
<label for="sponser">Sposer</label>
        
&lt;input type="radio" name="sap_radio2" value="my_sponser" &lt;?php echo set_radio('sap_radio2', '2'); ?&gt; /&gt;
<label for="my_sponser">No</label>
</p>
inside my model script

Code:
$data = array('sap_yes'=>$this->input->post('sap_yes'),
       'sap_no'=>$this->input->post('sap_no'),
       'sponser'=>$this->input->post('sponser'),
       'my_sponser'=>$this->input->post('my_sponser'));

my sap_yes, sap_no, sponser and my_sponser are the fields that i used inside the sap table.and the data type that is use is tinyint(2)..


Radio button - El Forum - 11-06-2012

[eluser]Rabbel[/eluser]
Hujan, in this case there will never be a post value 'sap_yes' and 'sap_no'. The identifiers will be the values of the NAME of your radiobuttons: 'sap_radio1' and 'sap_radio2'.

You should do this in your model class:

Code:
$data = array('sap_radio1'=>$this->input->post('sap_radio1'),
       'sap_radio2'=>$this->input->post('sap_radio2'));
//Check if values in array exist:
print_r($data);



Radio button - El Forum - 11-06-2012

[eluser]hujan[/eluser]
Code:
$data = array('sap_yes'=>$this->input->post('sapradio1'),
       'sap_no'=>$this->input->post('sapradio1'),
       'sponser'=>$this->input->post('sapradio2'),
       'my_sponser'=>$this->input->post('sapradio2'));

is this what you mean?


Radio button - El Forum - 11-06-2012

[eluser]Rabbel[/eluser]
Nope, you need to select your two names of your radio button groups: sap_radio1 and sap_radio2..


Radio button - El Forum - 11-06-2012

[eluser]hujan[/eluser]
i have tried
Code:
$data = array('sap_radio1'=>$this->input->post('sap_radio1'),
       'sap_radio2'=>$this->input->post('sap_radio2'));
//Check if values in array exist:
print_r($data);

but it give me this error
Code:
Array ( [sap_radio1] => sap_no [sap_radio2] => )
A Database Error Occurred

Error Number: 1054

Unknown column 'sap_radio1' in 'field list'

INSERT INTO `sap` (`sap_radio1`, `sap_radio2`) VALUES ('sap_no', 0)

Filename: C:\xampp\htdocs\CI\system\database\DB_driver.php



Radio button - El Forum - 11-06-2012

[eluser]Rabbel[/eluser]
The error you see there is an error where you try to put values in your database. Above the error you see the result of the print_r output:

Code:
Array ( [sap_radio1] => sap_no [sap_radio2] => )

Now you should use the values in this array to put in your database. The error you get says you do not have a column sap_radio1 in your sap table.


Radio button - El Forum - 11-06-2012

[eluser]noslen1[/eluser]
[quote author="hujan" date="1352205447"]
my sap_yes, sap_no, sponser and my_sponser are the fields that i used inside the sap table.and the data type that is use is tinyint(2)..[/quote]

You mean you have in your database 2 fields called "sap_yes" and "sap_no" ?
If that's the case, I think you should have just one field called something like "sap" that will be set to TRUE or FALSE, or 1 or 0.

I dont get the logic here...