Reputation: 15979
I have a small form, where I list an array $menu
as checkboxes.
Using jQuery (see script below) I then put the values of the checkboxes into an ( eventually hidden ) input field. I know this is not traditional way , But I want to store them all as a single united value.
When loading the form again (or after submitting) I of course need to populate those fields again with the value that was stored as a string ( from the input field ).
I use a simple strpos()
with a ternary operator for "checked"
.
My problem is that on every load , the FIRST value that is checked on the list , will NOT be confirmed and will not be checked . Always the first one
and only the first one
(which is upper in the list - and I do not mean the ABSOLUTE
first, but the first one to be previously checked . the first in the saved string.). Meaning that If I show the input field ( not hidden ), I can see the value in the string , but the checkbox is not "checked"
.
all the rest is working well....
Maybe only my eyes are tired , but I can really not find where the problem is :-)
Any input will be greatly appreciated ..
global $menu;
$output = '';
for ($i = 1; $i <= 100; $i++){
if ($menu[$i][2]){
$find = $menu[$i][2];
$pos = strpos($opp_array['brsa_menu_list'], $find);
$output .= '</br><input class="checkbox remove_check" type="checkbox" ' .(($pos) ? ' checked ':'') . 'name="option1" value="' . $menu[$i][2] .'"> ' . $menu[$i][2] ;
}
}
echo '<b>Available menus : </b><br>' . $output .'<br>';
?>
<p>
<input class="regular-text" id="sbrsamenu_list" name="brsa_s[brsa_menu_list]" type="text" value="<?php echo $opp_array['brsa_menu_list']; ?>"/>
<label class="description" for="sbrsamenu_list">
</br><?php echo 'Please write the menu items'; ?>
</label>
</p>
and the js
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function() {
var $checkboxes = jQuery(".remove_check");
$checkboxes.on('change', function() {
var ids = $checkboxes.filter(':checked').map(function() {
return this.value;
}).get().join(' | ');
jQuery('#sbrsamenu_list').val(ids);
});
});
// ]]>
</script>
Upvotes: 3
Views: 210
Reputation: 173652
If $pos
is meant to be a boolean value, indicating whether a string appears in another, you should compare the value from strpos()
against false
:
$pos = strpos($opp_array['brsa_menu_list'], $find) !== false;
Otherwise, if $find
appears at the start of your string, strpos()
returns 0
which in your code would be equivalent to false
.
Upvotes: 3