簡體   English   中英

Codeigniter中的數組到字符串的轉換錯誤

[英]Array to String Conversion error in Codeigniter

好吧,我在數組轉換中遇到了一些麻煩:

我的控制器:

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

var_dump($countryval);

我的觀點:

<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>

我從數組中的復選框獲取值,並且需要將該值作為字符串進一步傳遞。 我無法將值從數組轉換為字符串,即如果我var_dump($countryval)我在數組中獲取值! 請幫忙。 謝謝

那是因為您在輸入字段中使用了“ country []”作為名稱

如果您需要返回單個值,請嘗試此操作(在此處進行編輯,因為我輸入type =“ checkbox”而不是“ radio”。我已更正它)信任單個值的無線電。

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

希望能有所幫助

僅當“ country []”具有多個值時,才需要使用它作為名稱。 例如,對於多項選擇

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

您也可以使用input-checkbox。。但是,對於您的問題,我相信您只需要返回一個值即可。

好吧。現在從您的評論中我得到您想要的。 這是我的建議

1)當您具有多個具有相同名稱的復選框時,這些值將作為數組發送。

2)如果您一次只需要一個值並且肯定需要一個復選框,則可以使單選框看起來像這樣的復選框。

3)如果您確實要繼續使用javascript,一次只允許一個復選框,則可以執行此操作。

// 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

試試..!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM