This is my HTML code
<input type="checkbox" name="cbox[]" value="Jaywalking" />
Jaywalking<br>
<input type="checkbox" name="cbox[]" value="Littering" />
Littering<br>
<input type="checkbox" name="cbox[]" value="Illegal Vendor" />
Illegal Vendor
This is my PHP code
if(is_array($_POST['cbox'])) $violation=implode(',',$_POST['cbox']); else $violation=$_POST['cbox'];
mysql_query("insert into tblcitizen(violation) values ('$violation')",$conn) or die (mysql_error());
How can i update this checkbox? for example, i choose the jaywalking in my registration form, and i want to edit it, what do i need to do if i want it to be checked if i fetch it? please give a sample code. thank you much appreciated.
I would suggest to have another table related to tblcitizen as ManyToOne, so that you be able to create separate record for each submitted checkbox with the Id of the checkbox as value, then you will be able to update each record separately.
If you want to keep it in one table and save all value in one field separated by a delimiter and if the checkbox is not checked the value will not be save; for example if we have 3 checkboxes with these values jaywalking, littering, mysample and first, third ones are checked the value which is saved in Database should be jaywalking,mysample.
I assume this is your form on edit mode:
$result = mysqli_query("SELECT * FROM `tblcitizen` WHERE `id`='$id'");
$row = mysqli_fetch_assoc($result);
$selected_boxes = explode(',', $row['violation']);
<input type="checkbox" name="cbox[]" value="Jaywalking" <?php in_array('Jaywalking', $selected_boxes) ? 'checked' : '' ?> /> Jaywalking<br>
<input type="checkbox" name="cbox[]" value="Littering" <?php in_array('Littering', $selected_boxes) ? 'checked' : '' ?> /> Littering<br>
<input type="checkbox" name="cbox[]" value="Illegal Vendor" <?php in_array('Illegal Vendor', $selected_boxes) ? 'checked' : '' ?> /> Illegal Vendor
Now to edit after submit
if(is_array($_POST['cbox'])) $violation=implode(',',$_POST['cbox']); else $violation=$_POST['cbox'];
$query = "UPDATE `tblcitizen` SET `tblcitizen` = '$violation' WHERE `id`='$id'";
...
This is the simplest way to implement this logic
Related
Im currently in the making of adding different custom fields to a user in wordpress....
Adding simple text fields like a facebook, instagram field is easy enough, however when adding I want a checkbox group giving the user the possiblity to check multiple choices, im not sure on how to save it?
Lets say I have this HTML:
<input type="checkbox" name="sport[]" value="running" />Running<br />
<input type="checkbox" name="sport[]" value="soccer" />Soccer<br />
<input type="checkbox" name="sport[]" value="swimming" />Swimming<br />
How would I go about when saving the values to the database?
I've tried this when saving:
$userprofile_sports = (isset($_POST['sport']) && $_POST['sport'] <> '') ? $_POST['sport'] : '';
update_user_meta($userid, 'sport', $userprofile_sports);
I have created a contact form on my website using php and some of the values, more specifically the ones that have a checkbox-type input, are not send to my DB. I think there must be something wrong with the way I use the checkboxes or something. Upon submitting a form I get several notices like such:
Notice: Undefined index: ironing in
/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 79
Notice: Undefined variable: POST in
/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 94
The rest of the code seems to work fine and the data is loaded into my DB.
Here is some of my code in the demo-form.php file that I think is wrong:
<h4>i) Cleaningservice:</h4>
<p>Experience (in years): <input type="number" name="cleanExpInYears"/> </p>
<p>Skills: <br>
<input type="checkbox" name="basic" value="yes"> Basic cleaning<br>
<input type="checkbox" name="kitchen" value="yes"> Kitchen tools<br>
<input type="checkbox" name="laundry" value="yes"> Laundry<br>
<input type="checkbox" name="ironing" value="yes"> Ironing<br>
<input type="checkbox" name="window" value="yes"> Windows<br>
<p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
</p>
And here is the corresponding demo.php file that connects to the DB and stores the data there:
/* Cleaningservice data is gathered... */
$clean_value1 = $_POST['cleanExpInYears'];
$clean_value2 = $_POST['basic'];
$clean_value3 = $_POST['kitchen'];
$clean_value4 = $_POST['laundry'];
$clean_value5 = $_POST['ironing'];
$clean_value6 = $_POST['window'];
$clean_value7 = $_POST['other_clean'];
/* ...and inserted into the table cleaningservice */
$sql = "INSERT INTO cleaningservice (applicant_id, cleanExpInYears, basic, kitchen, laundry, ironing, window, other)
VALUES (LAST_INSERT_ID(),'$clean_value1', '$clean_value2', '$clean_value3', '$clean_value4', '$clean_value5', '$clean_value6', '$clean_value7')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
What am I missing here ??
Thanks in advance
The browser only passes the checkbox to the PHP script if the checkbox was actually checked.
So in PHP you have to check that the $_POST variable actually exists before using its contents, and provide the UNCHECKED i.e. default option if it does not exist.
/* Cleaningservice data is gathered... */
$clean_value1 = $_POST['cleanExpInYears']);
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = $_POST['other_clean'];
To avoid error messages like `array index does not exist you should check all POST/GET values exist like this anyway.
/* Cleaningservice data is gathered... */
$clean_value1 = isset($_POST['cleanExpInYears']) ? $_POST['cleanExpInYears'] : '';
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = isset($_POST['other_clean']) ? $_POST['other_clean'] : '';
You must switch between name and values
<p>Skills: <br>
<input type="checkbox" value="basic" name="yes"> Basic cleaning<br>
<input type="checkbox" value="kitchen" name="yes"> Kitchen tools<br>
<input type="checkbox" value="laundry" name="yes"> Laundry<br>
<input type="checkbox" value="ironing" name="yes"> Ironing<br>
<input type="checkbox" value="window" name="yes"> Windows<br>
<p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
</p>
and in PHP side, you will find your selected values in var_dump($_POST['yes'])
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
I'm creating some simple search page for my little project. Its about list of Multifunctional devices and options on devices.
I have table devices and fields with device options:
option_hdd
option_fax
option_df (document feeder)
option_duplex
and so on... totally 8 option fields.
If device contain options in field is 1 if not 0.
I created small form with check boxess for all this option with GET method.
<form action="search.php" method="get" name="search">
<input name="HDD" type="checkbox" value="1" /> HDD<br />
<input name="Fax" type="checkbox" value="1" /> Fax<br />
<input name="DF" type="checkbox" value="1" /> DF<br />
...
<input name="Search" type="submit" />
</form>
I try to create some query but i don't get exact results.
If user want to check for devices which contain hdd and fax option he will check that two checkboxes and hit search, query need to return results with devices that contain checked options (not essential for other options)
i try with fallowing query's i found googling :)
SELECT * FROM devices WHERE option_hdd = '$_GET[HDD]' OR option_fax = '$_GET[Fax]' OR option_finisher = '$_GET[Df]' ......'
and also
SELECT * FROM devices WHERE option_hdd = '$_GET[HDD]' AND (option_fax = '$_GET[Fax]' OR option_finisher = '$_GET[Df]' ......);
but i do'n get wanted results... but this last is the most close-up...
Example i have 5 devices in DB for testing
All 5 devices have Hdd and 2 devices have Fax option
In form i check hdd and fax and i get result of all 5 devices, but correct results must be 2... cause only two devices have fax option...
Can somebody help me and give me correct query :)
Thanks
I suggest using 'AND' but...
If the checkbox is checked, the value will be 1. If the checkbox is not checked the value will be empty.
So you code will be like this:
SELECT * FROM devices WHERE option_hdd = '1' AND option_fax = ' ' AND option_finisher = '1';
Which will give you a wrong resultset.
Try this instead:
$hdd = (!empty($_GET['HDD'])) ? $_GET['HDD'] : 0;
$fax = (!empty($_GET['Fax'])) ? $_GET['HDD'] : 0;
$df = (!empty($_GET['DF'])) ? $_GET['HDD'] : 0;
$sql = "SELECT * FROM devices WHERE option_hdd = '".$hdd."' AND option_fax = '".$fax."' AND option_finisher ='".$df."'";
Another option is to build your query. Change the name of the checkboxes like in the example below:
<form action="" method="get" name="search">
<input name="option[HDD]" type="checkbox" value="1" /> HDD<br />
<input name="option[Fax]" type="checkbox" value="1" /> Fax<br />
<input name="option[DF]" type="checkbox" value="1" /> DF<br />
...
<input name="Search" type="submit" />
</form>
And see the phpexample:
$sql = 'SELECT * FROM devices';
if(isset($_GET['option']) && count($_GET['option']) > 0){
$c = 0;
$sql .= ' WHERE';
foreach ($_GET['option'] AS $key => $option) {
$sql .= ($c == 0) ? ' option_'.$key.'='.$option : ' AND option_'.$key.'='.$option;
$c++;
}
}
Another solution is to use this trick:
<input name="HDD" type="hidden" value="0" />
<input name="HDD" type="checkbox" value="1" /> HDD<br />
This way if PHP will revert to default 0 if it doesn't get the value from checkbox.
And please be careful with your data, don't use $_GET directly in SQL.
I have a html form where there are two Input fields
say Caste (options are SC, ST and OBC) and Direction (options are north, south, east and west). Users will choose the options using check box.
<input name="ct" type="checkbox" value="SC">SC
<input name="ct" type="checkbox" value="ST">ST
<input name="ct" type="checkbox" value="OBC">OBC
<input name="dr" type="checkbox" value="N">North
<input name="dr" type="checkbox" value="S">south
<input name="dr" type="checkbox" value="E">East
<input name="dr" type="checkbox" value="W">West
I have also a database (name : PEOPLE) with column name Caste , Direction etc.
Now I want to run a sql query based on user's selection. For example
mysql_query("select * from PEOPLE where Caste='option/options selected by user' and Direction= 'option/options selected by user' ")
If user choose one option from each field then it is not a problem for me,
mysql_query("select * from PEOPLE where Caste='$_POST[ct]' and Direction= '$_POST[dr]' ")
but if they use multiple options , then how should I proceed.
Give name attribute to the checkboxes like this
<input type="checkbox" name="caste[]" value="SC" />
<input type="checkbox" name="caste[]" value="ST" />
<input type="checkbox" name="caste[]" value="OBC" />
On you php side you can use function implode to form caste into a string as shown below (considering you are doing a POST)
$caste_id = implode(",",$_POST["caste"]);
Where you read from the database you can transform the value from db to an array like this
$caste_array = explode(",",$row->caste_id);
I hope this helps
You can use WHERE IN
$caste_string = implode('","', $_GET['ct']);
$caste_string = '"'.$caste.'"';
$dir_string = implode('","', $_GET['dr']);
$dir_string = '"'.$caste.'"';
mysql_query("select * from PEOPLE WHERE Caste IN ($caste_string) AND Direction IN ($dir_string)")