Add checkbox value to multiple fields in database - php

I have a checkbox that if checked stores the value "testing" to the field "checkbox" om my database.
<input type="checkbox" name="checkbox" value="testing">
I would like to store two values, on two fields on my database, from one checkbox.
If checked:
store value testing to the checkbox field
store value testing2 to the checkbox2 field
Is this possible?
Thanks in advance

<input type="checkbox" name="checkbox" <?php if($val=="testing"): echo "value='testing' echo checked='on'" ?>">
<input type="checkbox2" name="checkbox2" <?php if($val=="testing2"): echo "value='testing2' echo checked='on'" ?>">
Hope this is what you are looking for..

Related

How to take multiple data at checkbox value in php?

Here I just grabbed the ID or a data in the check box. But I want to capture more data with the ID. How to do it?
<input type="checkbox" name="man" value="<?php echo $row['id']; ?>">
And how do I insert that data into another table via PHP MySQL?
You have to set checkboxes name with array brackets.
Ex: <input type="checkbox" name="man[]" value="<?php echo $row['id']; ?>">
After that if you get their values with $_POST["man"], then an array with value of the checkboxes will be return.

Checkbox and MySQL database with PHP

I've got a problem.
I don't know how to put the value of my checkbox in my MySQL database with PHP.
So :
<input type="checkbox" name="checkiard" value="IARD">
How to put the value "IARD" in my database ONLY if the checkbox is check ?
Thank you guys.
You can access the value of checkbox as any other input types. Its simply
if(isset($_POST['checkiard'])) {
$optionArray = implode(",", $_POST["checkiard"]);
}
Try this.
If you have more than one checkbox then you markup should be something like this
<input type="checkbox" name="checkiard[]" value="IARD-1">
<input type="checkbox" name="checkiard[]" value="IARD-2">
<input type="checkbox" name="checkiard[]" value="IARD-3">
you must keep the name same for all the checkbox.

Get several checkboxes id's after submission

I have code similar to the following:
<input type="checkbox" name="visitProperty" value="1" id="visit-0">
<input type="checkbox" name="visitProperty" value="1" id="visit-1">
<input type="checkbox" name="visitProperty" value="1" id="visit-2">
...
Once the form is submitted I want to get checked checkboxes, so far I've been using
if (isset($_POST['visitProperty']) {..}
But to my understanding it only gets one checkbox? Where as I need to check all of them and see if they were checked, so inside the if statement I can create a loop that gets id's of all submitted checkboxes and then gets the number from id, to update a certain array.
<input type="checkbox" name="visitProperty[]" value="1" id="visit-0">
<input type="checkbox" name="visitProperty[]" value="2" id="visit-1">
<input type="checkbox" name="visitProperty[]" value="3" id="visit-2">
<?php
foreach($_POST['visitProperty'] as $check) {
echo $check . "<br>"; // for example
}
?>
NOTE: $_POST['visitProperty'] will hold checked checkbox values. You will access all the checkboxs as an array as following $_POST['visitProperty'][]
When you put in the name, you are declaring a variable. You need to declare it as an array, or each checkbox will bump out the last one. Add some empty square brackets to the name.
You would need or defined ID or a unique value, otherwise you will not be able to identify them on the server-side (the ID does not get sent in $_POST).
So in case of a unique, identifyable ID, you could do something like:
<input type="checkbox" name="visitProperty[<?php // echo some unique id from a database for example ?>]" value="1" id="visit-0">
The reason you would need the ID to be identifyable, is that unchecked checkboxes do not get sent to the server, so you might end up with an array of 2 if visitProperty is an array, but you would not know which 2.

Display mysql checkbox value in form

A simple question but I can't seem to find the answer.
I have a simple form that inserts the value 'on' to a database when a checkbox is checked
How do I get the checkbox checked when the data is pulled from the database when the form is revisited and displaying with the database information?
I have tried this but it doesn't work
<input type="checkbox" name="positioning" class="input_margin" value="<? $row['positioning']; ?>">Positioning<br />
(I only need help with this, I have the sql query etc set up fine)
Thanks
Use a conditional statement to check the database value and echo 'checked' if it should be checked
<input type="checkbox" name="positioning" class="input_margin" <? if($row['positioning']=='on'){echo 'checked';} ?>>
You need to use the checked property and not value.
The value field is what is provided if the box is checked (e.g positioning=on) for value="on"
<input type="checkbox" name="positioning" class="input_margin" <? if($row['positioning']) { ?> checked<? } ?>/>Positioning<br />
Include the value attribute so the checkbox input will post "on" if checked.
Otherwise the form will initially retrieve a value but fail to post on any subsequent form submission. to the database
<input type="checkbox" name="positioning" class="input_margin" value="on" <? if($row['positioning'] =='on'){echo 'checked';} ?> />Positioning<br />

Count number of Checked check boxes from a php gathered check list

I'm presented with a problem using PHP and MYSQL. I have a dynamic list of options which the user can select (maximum of 3) that are added from the administration panel as shown below:
<input type="checkbox" name="<?php echo "category".$i; ?>"
value="<?php echo $cat_id; ?>" />
<?php echo $cat_name; ?><br />
<?php
$i++;
}
echo "<input type='hidden' name='num_cat' value='$num_cat' />";
?>
I now want to count how many check boxes are 'checked' and if there are more than 0 and less than 4 checked it will update the mysql table with these stored. They are stored by means of 1's and 0's. So they tick 'yes' and a 1 is stored, they tick 'no' and a 0 is stored.
I've been trying to use jQuery and Javascript but they all seem to be for Check Box Forms which have the values pre-written within a form, mine are dynamic from a database.
Many thanks for your help.
Use [] in the end of your checkbox names and use the sizeof function on the corresponding $_POST[] array in your php script.
<input type="checkbox" name="values[]" value="1" />Value 1
<input type="checkbox" name="values[]" value="2" />Value 2
sizeof($_POST['values']);
Note that the checkboxes have the same name and end with brackets (values[]). This indicates that the checkboxes belong together and are bundled as an array in php. Only the selected values will be present in the array as well.

Categories