Select Value from DB Array inside check box input - php

Hello I am using various check boxes with different values and than storing the vales inside DataBase as all the checkboxes values are store inside Array
<input type="checkbox" value="Gym" id="details_10" name="utilities[]">
<input type="checkbox" value="Spa" id="details_11" name="utilities[]">
and than store in the database like: Gym, Spa.
Later I am making MySQL query inside edit form where I will be able to edit those and other parameters. For the other inputs I am using:
<?php if($row['data'] == 'Sample Data'){echo 'selected="selected"';}?>
In this case I would like to use something like the following:
<input type="checkbox" value="Gym" id="details_10" name="utilities[]"<?php if($row['utilities'] == 'Gym'){echo 'checked"';}?>>
Any help to achieve this result will be very welcome.

I've always used this in these cases.
<?php
if($row['utilities'] == 'Gym')
{
?>
<input type="checkbox" value="Gym" id="details_10" name="utilities[]" checked="checked">
<?php
}
else
{
?>
<input type="checkbox" value="Gym" id="details_10" name="utilities[]">
<?php
}
?>

Related

Getting check box values as checked from database codeigniter

I'm trying to get check box values inserted into my database as checked.. I'd inserted it using the implode method as string. It inserted the values successfully., but my condition to get the check box value "checked" is not working..
<label>Some text</label>
<input type="checkbox" name="text[]" value="text1"
<?php echo set_checkbox('text', $row->Some_text)== 'text1' ? "checked" : "";?>>text1
<input type="checkbox" name="text[]" value="text2"
<?php echo set_checkbox('text', $row->Some_text)== 'text2' ? "checked" : "";?>>text2
<input type="checkbox" name="text[]" value="text3"
<?php echo set_checkbox('text', $row->Some_text)== 'text3' ? "checked" : "";?>>text3
<input type="checkbox" name="text[]" value="text4"
<?php echo set_checkbox('text', $row->Some_text)== 'text4' ? "checked" : "";?>>text4
I wrote model for edit as :
public function edit($id)
{
$sometext = $this->input->post('text');
$data=array(
'Some_text'=>json_encode(implode(",", $sometext)),
);
$this->db->set($data);
$this->db->where('User_id',$id);
$this->db->update('tbl_check');
$query = $this->db->get('tbl_check');
return $query->row();
}
And edit is working well..
Sometimes the use of functions from frameworks just make it totally unnecessarily and dirtier.
<input type="checkbox" name="text[]" value="text1" <?php echo ($yourVar == 'text1' ? 'checked' : null); ?>>
Just get the data from the Model. and pass it to the check box page.
$data['check_box_data']=$query->row_array();
$this->load->view('page',$data);
View:
<?php
$c_box1=$c_box2=$c_box3='';
$chk_data=explode(',',$chk_box_data); // $chk_box_data is which is from DB
foreach($chk_data as $list)
{
//chk_box1_value1,2,3 are original check box values
if($list=='chk_box1_value'){$c_box1='checked';}
if($list=='chk_box2_value'){$c_box2='checked';}
if($list=='chk_box3_value'){$c_box3='checked';}
}
?>
label>Some text</label>
<input type="checkbox" name="text[]" value="text1"
<?php echo $c_box1;?>>text1
<input type="checkbox" name="text[]" value="text2"
<?php echo $c_box2;?>>text2
<input type="checkbox" name="text[]" value="text3"
<?php echo $c_box3;?>>text3
Try this....
Using standard CodeIgniter functions:
form_checkbox('fieldname', '1', set_checkbox('fieldname', '1', (TEST YOUR DB DATA HERE)));
Example:
form_checkbox('fieldname', '1', set_checkbox('fieldname', '1', (!empty($data_from_db))));
Explanation:
set_checkbox() determines whether to output checked="checked" and thus show your field as checkmarked or not.
The third parameter of set_checkbox() determines whether the initial state of your checkbox will checked/not-checked. If the third parameter evaluates to TRUE the first time the form is loaded, the checkbox will be ticked; if it evaluates to FALSE, it won't be marked.
So, when editing data from a table, use the Controller to get the data from the table and pass it to the form. In your form, test the data from your table in the third parameter of set_checkbox().
In my example above, the data for FIELDNAME was stored in the table as "1" or "0".
When the edit form is loaded, the third parameter of SET_CHECKBOX tests, "Is the data from the table empty?" If there is data, !empty() returns TRUE, causing set_checkbox() to set the DEFAULT STATE to CHECKED.
The key is using the THIRD PARAMETER of set_checkbox() to evaluate your existing value.
You are using set_checkbox incorrectly. Look at the documentation: do not use this in a conditional statement. Your question doesn't give much information, but I assume you want something like this:
<input type="checkbox" name="text[]" value="text1"
<?php echo set_checkbox('text[]', 'text1');?>>text1
<input type="checkbox" name="text[]" value="text2"
<?php echo set_checkbox('text', 'text2');?>>text2
<input type="checkbox" name="text[]" value="text3"
<?php echo set_checkbox('text', 'text3');?>>text3
<input type="checkbox" name="text[]" value="text4"
<?php echo set_checkbox('text', 'text4');?>>text4

How to write a conditional statement with a value but only if it's the only value?

if($uriarray['value'] == 0 ){
I have an input form which includes values. The values are in a checkbox. I want to show something if it's the only value, but not if it's included with other values. So if the value is 0 I want to show something but if it includes other values like 1,2,3 etc, I don't want to show it. Is this something that's possible?
If & only if?
It depends on your HTML form. For example, if you specify the names of the checkboxes in array-form like:
<input type="checkbox" name="uriarray[]" value="0" />
<input type="checkbox" name="uriarray[]" value="1" />
<input type="checkbox" name="uriarray[]" value="2" />
<input type="checkbox" name="uriarray[]" value="3" />
you would receive an array in PHP with all checked values.
<?
if (count($_POST['uriarray']) == 1) {
$onlyValues = $_POST['uriarray'][0];
}
?>
If you also want to check the single value to be 0:
<?
if ((count($_POST['uriarray']) == 1) && ($_POST['uriarray'][0] == "0")) {
//Do something
}
?>

checkboxes in php form doesn't work

How can I use the conditional OR in a form with isset?
I have this but it does not work.
FORM HTML:
...
<input type="checkbox" name="carga1">
<input type="checkbox" name="carga2">
...
and the PHP
$cargas=array($_POST['carga1'],$_POST['carga2'],$_POST['carga3'],
$_POST['carga4'],$_POST['carga5'],$_POST['carga6'],
$_POST['carga7'],$_POST['carga8'],$_POST['carga9'],
$_POST['carga10'],$_POST['carga11'],$_POST['carga12'],
$_POST['carga13'],$_POST['carga14'],$_POST['carga15'],
$_POST['carga16'],$_POST['carga17'],$_POST['carga18']);
if(isset($cargas[0]) ││ isset ($cargas[1])){
$cargas[0]=5.62;
$cargas[1]=4.5;
echo "$cargas[0]<br>";
echo "$cargas[1]<br>";
}
i expect that this works but is not.
Only checked checkbox is posted to the server.You have to change your condition using pregmatch and work accordingly.
$postData = $_POST;
foreach ($postData as $key => $value) {
$match = preg_match('|cargas(\d+)|', $key, $matches);
if ($match) {
$index = $matches[1];
if($index == 0 || $index == 1){
// do your stuff which you would have done in case of $cargas[0] ,$cargas[1]
}
}
}
I think array is not Suitable way to do this try following
try this
<input type="checkbox" name="carga1">
<input type="checkbox" name="carga2">
.....................................
<input type="submit" name="submit">
<?php
if(isset($_POST['submit'])){
//
$category1=$_POST['carga1'];
$category2=$_POST['carga2'];
$category3=$_POST['carga3'];
if(isset($category1) ││ isset ($category2)){
$category1=5.62;
$category2=4.5;
echo "$category1<br>";
echo "$category2<br>";
}
}
?>
only the checked checkboxes get posted. so it needs slightly different appraoch.
You can acheive it like this-
put a hidden input with the same name as the checkbox that might not be checked. I think it works so that if the checkbox isn't checked, the hidden input is still successful and sent to the server but if the checkbox is checked it will override the hidden input before it. This way you don't have to keep track of which values in the posted data were expected to come from checkboxes.
<form>
<input type='hidden' id='testName' value='0' name='carga1'>
<input type='checkbox' id='testNameHidden' value='1' name='carga1'>
</form>
Before submitting the form , disabled the hidden field based on the checked condition.
<script>
if(document.getElementById("testName").checked){
document.getElementById('testNameHidden').disabled = true;
}
</script>
I personally think its the easiest approach for this.
ok, check boxes in html works as follows,
<input type="checkbox" name="carga1" value="1">
<input type="checkbox" name="carga2" value="123">
in php,
if the check box is in checked state during the submission, you will get
isset($_POST['carga1']) as true, else the form element would not be available in post data, hence false.
and in cheked state you will get value for
$_POST['carga1'] as 1 and
$_POST['carga2'] as 123
and if you want to group the check boxes in form you can use a single name for multiple check boxes and different values,
<input type="checkbox" name="carga[]" value="1">
<input type="checkbox" name="carga[]" value="2">
<input type="checkbox" name="carga[]" value="3">
<input type="checkbox" name="carga[]" value="4">
and in php you will get an array of selected values of the check boxes
$arr=$_POST['carga'];
and you can use foreach to iterate through the values,,,

Checkbox input array values passed to insert into MySQL query based on selections

My array of checkbox inputs is displaying and working perfect:
<input name="seminar[<?php echo $a; ?>]" type="checkbox" id="seminar_<?php echo $a; ?> "value="" <?php echo $checked; ?>>
This is my MySQL insert data ($s):
if ($seminar) {
foreach ($seminar as $s )
$interest .= "$s ";
}
My earlier for loop that processes the checkbox inputs above, does not store the data in $s. And I am trying to find a way to store those values to pass to that foreach.
<input type="checkbox" name="apple[]" value="1">
<input type="checkbox" name="apple[]" value="2">
<input type="checkbox" name="apple[]" value="3">
your checkbox is something like this...............ok.
then in php you just post the variable
<?php $apple=$_POST['apple'];
?>
now the $apple is an array of your selected values.
is it ok for your project.....................

How to remember checkbox input in PHP Forms

For usability purposes I like to set up my form fields this way:
<?php
$username = $_POST['username'];
$message = $_POST['message'];
?>
<input type="text" name="username" value="<?php echo $username; ?>" />
<textarea name="message"><?php echo $message; ?></textarea>
This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.
My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?
My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.
If that is not an option, then try this:
<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />
So if subscribe is = 1, then it should select the box for you.
For Example, consider the following code for checkbox :-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
HTML<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
Javascript<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />
And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-
$course = array();
I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.
I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...
CODE IN PHP
$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;
<b><label for ="checkbox">New Arrival</label></b>
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> (Tick box if product is new) <BR><BR>
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>
Also, please consider sanitising your inputs, so instead of:
$somevar = $_POST['somevar'];
...it is better to use:
$somevar = htmlspecialchars($_POST['somevar']);
When the browser submits a form with a checked checkbox, it sends a variable with the name from the name attribute and a value from the value attribute. If the checkbox is not checked, the browser submits nothing for the checkbox. On the server side, you can handle this situation with array_key_exists(). For example:
<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />
Using array_key_exist() avoids a potential array index undefined warning that would be issued if one tried to access $_POST['myCheckbox'] and it didn't exist.
You may add this to your form:
<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />
isset checks if a variable is set and is not null. So in this code, checked will be added to your checkbox only if the corresponding $_POST variable has a value..
My array has name="radioselection" and value="1", value="2", and value="3" respectively and is a radio button array... how to I check if the radio value is selected using this code
I tried:
<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />

Categories