0

Well I am Stuck some where here in Array conversion:

My Controller:

$username = $this->input->post('FirstName');
$countryval= $this->input->post('country');

var_dump($countryval);

My View:

<input type="text" name="FirstName" id="Name" >
<?php
foreach ($countryDetails as $row )
{
    echo '<input id="country" type="checkbox" name="country[]" class="unique"  value="'.$row->country_id.'">'.$row->country_name.'<br/>';
}
?>
<script>
    $('input.unique').click(function() {
        $('input.unique:checked').not(this).removeAttr('checked');
    });
</script>

I am getting the value from checkbox in an array and I need to pass that value as a string further. I am not able to convert value from array to string i.e if I var_dump($countryval) I get value in array ! Please Help. Thank you

2
  • Please show the output of var_dump($contryval);. Commented Jan 8, 2014 at 12:38
  • on selecting 1st check box i get output as--array(1) { [0]=> string(1) "1" } on selecting 2nd checkbox--array(1) { [0]=> string(1) "2" } Commented Jan 8, 2014 at 12:40

1 Answer 1

1

That is because you have used "country[]" as the name in input field

Please try this incase you need a single value to be returned (Edit made here coz I put type="checkbox" instead of "radio".. I have corrected it) Trust radios for single values.

echo '<input id="country" type="radio" name="country" class="unique"  value="'.$row->country_id.'">'.$row->country_name.'<br/>';

hope that helps

You need "country[ ]" as name only if it has multiple values. Eg., For a multiple select

<select name="country[]" multiple>
  <option></option> <!-- Your options go here-->
</select>

You can use input-checkbox too.. But with your question, I believe you need just a single value to be returned.

Ok.. Now From Your comments I kind of get what you want. Here is my suggestion

1) When you are having multiple checkboxes with the same name, the values are sent as an array.

2) If you would need a single value at a time and definitely need a checkbox, You can make a radio look like a checkbox like this.

3) If you really want to proceed with your javascript allowing only one checkbox at a time, you can do this.

// Load the 'array' helper
$this->load->helper('array');
// Use the 'element' function to return an element from the array
$countryval = element('0', $this->input->post('country')); //should work

Have a try..!!

Sign up to request clarification or add additional context in comments.

8 Comments

even after doing so i am getting the same results!
is the var_dump giving the same output or a different one..?
i am getting same output.
Well.. The question is, if you have a multiple checkbox, with the same name, they should be giving out an array. So tell us whether you need a single value or a multiple values from that form?
I believe my edit should help you out getting a single country out. tell me if You need something else and if I am wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.