Custom user fields, saving multiple values from checkbox group - php

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);

Related

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.

Checkbox module, how to save, edit, and update. (Beginner)

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

Pulling information from a database into checkbox value

I have a website where users upload information into a database via form. I also have an update page where users can edit and update information they have already put on the database. The update page is virtually identical to the upload page. The main difference is that is has information from the database as values.
Now a section of this form is checkboxes. I am currently trying to extract the information from the database for the checkbox fields.
Here is what I have right now.
<input type="checkbox" name="utilities[]" value="Television" <?php if ($varUtilities == 'Television') echo "checked='checked'"; ?>/>Television
<input type="checkbox" name="utilities[]" value="Internet"<?php if ($varUtilities == 'Internet') echo "checked='checked'"; ?>/>Internet
<input type="checkbox" name="utilities[]" value="Electricity (hydro)"<?php if ($varUtilities == 'Electricity (hydro)') echo "checked='checked'"; ?>/>Electricity(hydro)
<input type="checkbox" name="utilities[]" value="Gas"<?php if ($varUtilities == 'Gas') echo "checked='checked'"; ?>/>Gas
Works fine if you only choose one checkbox
Problem: What if you check two or all of them. When I upload the check box I upload as an array. How do I adjust this so It can read the array and pick up "Television" from [Television, Internet, Gas] or Pick up "Gas" from [Television, Internet, Gas]

How to store data if checkbox is selected

I'm doing this one using PHP.
I'm doing some basic quiz application and it has a backend. On my backend, I am setting up a quiz for students to answer. One of my interface is like this.
If the user checked the box, it means 1950 is the answer. How would I process that one in my database to determine that 1950 has been the answer of the question.
My Database is like this.
tbl_choices
Id,Choice,isAnswer
so ideally, it would be stored like this.
tbl_choices
ID CHOICE isAnswer
001 1900 0
002 1800 0
003 1950 1
004 1850 0
My question here is how would I code it in a sense when a user will check the checkbox and the textinput right beside it will have a value of isAnswer as 1.
Just an additional info: When a user will click that + button it will add a new textinput and if user will click - button it will delete a textinput, but I got that all covered.
The choices are dynamic, it will changed, that above that I've shown you is just an example.
P.S: Sorry for the title, I don't know what's the title of this kind of question :-)
Your help would be greatly appreciated and rewarded!
you can try html like this :
<input type="checkbox" value="1900" name="answer1[]">
<input type="text" value="1900" name="answer2[]" />
<input type="checkbox" value="1800" name="answer1[]">
<input type="text" value="1800" name="answer2[]" />
<input type="checkbox" value="1950" name="answer1[]">
<input type="text" value="1950" name="answer2[]" />
<input type="checkbox" value="1850" name="answer1[]">
<input type="text" value="1850" name="answer2[]" />
and then use php code:
foreach($_POST['answer2'] as $v){
if(in_array($v, $_POST['answer1'])) {
$s = 1;
}else
$s = 0;
$sql = "INSERT INTO table VALUES(null, $v, $s)";
}
also on click plus update values of both check box and text field
in general you can use checkboxes as an array when submiting information, just name the checkboxes as an array like
<input type="checkbox" value="1900" name="answer1[]">
<input type="checkbox" value="1800" name="answer1[]">
<input type="checkbox" value="1950" name="answer1[]">
<input type="checkbox" value="1850" name="answer1[]">
then on the server side you handle $_POST["answer1"] as an array and cycle through the possible answers, something like this
if (is_array($_POST["answer1"]))
{
foreach($_POST["answer1"] as $answer)
{
// insert into database here
}
}
important: this will only show what a user actualy selected, if an option is not clicked then it will not be part of the array
I think it would make sense to have more than one table, one for the questions with their options and then one for the answers with the fields id, choice_id. Otherwise it is going to make generating the questions a lot more difficult especially when a number of students answer the same question?
One more thing, you could add up in the answers of your question, add row and delete row based on + and - button.
The code for that:
<script type="text/javascript">
$(document).ready(function() {
$("#plus").click(function() {
$('#mytable2 tbody>tr:last').clone(true).
insertAfter('#mytable2 tbody>tr:last');
$('#mytable2 tbody>tr:last #st').val('');
$('#mytable2 tbody>tr:last #newst').val('');
$('#mytable2 tbody>tr:last #plus').val('+');
return false;
});
});
for more detail, you may refer following link:
Add Row on Button click

two values in one field

I've this simple form, when the price is set to 120 $:
<input type="checkbox" name="addon[book]" value="120" /> book
<input type="checkbox" name="addon[plane]" value="420" /> Plane
Could I send two values (USD & €) with thr form?
EDIT:
I added name="addon[book]" because I'll display the name too
EDIT 2:
PHP:
foreach($addon as $name => $value) {
echo 'your addons: '.$name;
}
$total = array_sum(array_map("intval", $_POST["addon"]));
echo 'the total '.$total.' $';
<input type="hidden" name="addon[book_USD]" value="250" />
<input type="checkbox" name="addon[book_EUR]" value="120" /> book
However NEVER trust the input from the form. Check the price in the backend code or users can change the price!
EDIT
What about json encoding?
<input type="checkbox" name="addon[book]" value="{"USD":250,"EUR":120}" /> book
EDIT2
To explode the values you'll have to use json_decode()
EDIT3
As others (and myself) already stated what you are trying to do (at least from the looks of it) is that you use the price from the input field as the price the user is going to pay.
That is not the best thing you can do, cause users can easily change the price of the input field.
I don't know your code but somehting like the following would be much better:
<input type="checkbox" name="addon[book]" value="book1">
And in your PHP code:
$prices = array('book1'=>array('USD'=>250, 'EUR'=>120),
'book2'=>array('USD'=>120, 'EUR'=>60),
);

Categories