Retrieve Checkbox Value that has been stored on DB - php

I've been stored my checkbox value into DB using this code
$request->merge([
'revisi_dokumen' => implode(',', (array) $request->get('revisi_dokumen'))
]);
But now i can't get it back when i try to edit(in editpage.blade), i want the checkbox is checked if it's value same as value in DB, how do do that guys ? please kindly help me solve

You used implode() to store, use explode() to retrieve.
$values = explode(",", $model->revisi_dokumen);
In your view you check if the checkbox value is in the array
<input type="checkbox" name="revisi_dokumen[]" value="1" {{ in_array('1', $values) ? 'checked' : '' }}>One

Related

How to have checkbox items create a list

I am new to PHP and the project I am working on is a codeigniter php form writing back to a FileMaker database.
The checkboxes should be populating as a list field.
Here is my form
<input name="fieldname[]" value="value1"<?php echo (set_checkbox('fieldname', $join->fieldname))?> type="checkbox" class="form-check-input"> value1
<input name="fieldname[]" value="value2"<?php echo (set_checkbox('fieldname', $join->fieldname))?> type="checkbox" class="form-check-input"> value2
There are about 7 checkboxes.
I assume that I have to setup a foreach loop to get it to go thought to FileMaker's field, but unsure how to do that.
If I take away the array, the last one selected goes through to the layout.
Any help would be great.
Presumably, these values mirror a valueList back in FileMaker. If so, it's best to assign the valueList to a variable:
$values = $layout->getValueList('pizzaToppings');
And use that in your for loop:
foreach($values as $value)...
In your if(isset()) block, post a return-delimited array to FileMaker:
// value(s)!
if ($_POST['fieldname']) {
$valueArray = implode("\r", $_POST['fieldname']);
}
// pass the array to setField()
$record->setField('filemakerField', $valueArray);
I worked it out. To enter checked boxes into a return separated list:
View:
At the top of the section with the checkboxes
<?php $join->fieldname = explode("\n", $join->fieldname); ?>
// The actual input in the form
<input name="fieldnameXX[checkboxValue]" value="value" <?php echo (in_array('value', set_value('fieldname[value]', $join->fieldname)) ? ' checked="checked"': '')?> type="checkbox" class="form-check-input" > Value
Factory:
$fieldnameZZ = $data['fieldnameXX'];
$data['fieldnameXX'] = FALSE;
unset($data['fieldnameXX']);
$sacraments = implode("\n", $fieldnameZZ);
$data['fieldname'] = $fieldnameZZ;
I'm not sure if this is the best way to have done it, but it works.
$ids=implode(",", $_REQUEST["fieldname"]);
$result3=mysqli_query($dbh,'SELECT* FROM excel_tenant WHERE ID IN ("' .
$ids . '") AND
ManagerID = "'.$_SESSION["ManagerID"].'" ORDER BY ID DESC ') or
die(mysqli_error($dbh));

How to store selected multiple checkbox values in an array in php?

<input type="checkbox"name="travel[]" value="bus"/>
<input type="checkbox"name="travel[]" value="train"/>
<input type="checkbox"name="travel[]" value="plane"/>
foreach($_POST['travel']as $selected)
var select[]=$selected;
If the user selects all the three checkboxes, I have to store them in an array and send it to mail as I dont have a data base. So how should I store them in an array?
foreach($_POST['travel']as $selected)
var select[]=$selected;
The above code is returning only last selected check box
And how should I pass it and display it on the mail?
Instead of
foreach($_POST['travel']as $selected)
var select[]=$selected;
update it to
$select = array();
foreach($_POST['travel'] as $key => $selected){
$select[$key]=$selected;
}
Instead of using foreach simply use $select = implode(',',$_POST['travel']);
Because every time you are defining a new array, by var select[]=$selected;.
Change it it $select[]=$selected;
please give different names as below.
after posting data using foreach loop you will get all selected options
<input type="checkbox" name="travel1[]" value="bus"/>
<input type="checkbox" name="travel2[]" value="train"/>
<input type="checkbox" name="travel3[]" value="plane"/>

Array to String Conversion error in Codeigniter

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

Fill the checkboxes from an array in PHP

Everyone
My question is so clear I think:
I have an array of checkboxes in HTML
<input type="checkbox" name="gruplar[]" value="<?=$deptId?>">
It takes "value" values from database ID's e.g. 1,4,5
I can get these values in an array variable named $divided[]. For example 1,4,5 selected and the array is: $divided[0]=1,$divided[1]=4,$divided[2]=5,
When I need to update the record of checkbox list, I want to get this checkbox list selected with these array values.
Check the checkbox which has "value" of "1", "4" and "5" according to
the elements of the array.
How can I do that in HTML mixed PHP context?
<input type="checkbox" name="gruplar[]" value="<?=$deptId?>"<?=(in_array($deptId,$divided))?' checked="checked":''?> />
This checks if the value of the checkbox (as given by $deptId, which I'm assuming is what you meant as you're echoing it for the value attribute) is in the array $divided. If it is, it echos markup to make the checkbox checked when it is rendered by the browser.
<?php
$checked = array(1, 4, 5);
foreach ($all as $id) // $all is all checkboxes
{
sprintf('<input type="checkbox" name="gruplar[]" value="%d" %s>', $id, in_array($id, $checked) ? 'checked="checked"' : '');
}
?>
Also, it's better to build meta array like this
array(
//(int) id => (bool) checked
1 => true,
2 => false,
...
);
Then do:
foreach ($meta as $id => $checked)
{
sprintf('<input type="checkbox" name="gruplar[]" value="%d" %s>', $id, $checked ? 'checked="checked"' : '');
}
For Update ur Checkbox Field will display the PHP if else condition for comparing two variables for mapping
if(in_array($depid,$divided)){
$checked = "checked='checked'";
}else{
$checked = "";
}
<input type="checkbox" name="gruplar[]" value="<?=$deptId?>" <?=$checked; ?>/>

How to retrieve information from Check Box?

My problem is a little bit complicate. (I use PHP)
I have two arrays, (simple arrays array[0] = string, array[1] = string...)
OK, now I will display the content of two arrays in a webpage.
The first array contain names and the second images URL.
Images and names are already displayed (my problem isn't here).
But now I want to do something else, add a check box near every image, those checkbox r active by default. Ok, now the user can uncheck some inbox;
The final aim, is to get a new array containing only the values of the names and images that had been checked.
I have thought of something simple, crawl the keys (number) of unchecked checkboxes and unset them from my array. But the problem that I didn't know how to deal with the check boxes
To receive inputs as arrays in PHP, you have to set their name using brackets in HTML:
<label><input type="checkbox" name="thename[]" value="" /> The text</label>
Then, when you access $_REQUEST['thename'] you'll get an array. Inspect it to see its format and play with it :)
first of all i recomend having just one array:
$array = array (0 => array('name' => '....', 'url' => '....'))
i think this will make your life much easier.
Also in the HTML you could also send the array key
foreach ($yourArray as $key=>$value) {
...
<INPUT type="checkbox" name="chkArr[<?php echo $key ?>]" value="1" checked/>
then in your form action you itarate the intial array and remove the unchecked ones.
foreach ($yourArray as $key=>$value) {
if (!isset($_POST['chkArr'][$key]) OR $_POST['chkArr'][$key]!='1') {
unset($yourArray[$key]);
}
}
<INPUT type="checkbox" name="chkArr[]" value="$num" checked/>
After the form is submitted, you'll have array $_REQUEST['chkArr'], in which you'll have numbers of the checkbox that are still checked.
To see which have been unchecked use array_diff($listOfAllNums, $chkArr)

Categories