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]
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 am storing service_name array for my 9 check boxes. This is my code for checkboxes.
<?php
$service_name=get_post_meta($edit_id, 'service_name',true);
?>
<input type="checkbox" class="service_name" size="80" name="service_name[]"
value="architecture"
<?php if(in_array('architecture',(array)$service_name) ){
echo "checked";
} ?>
onclick='chkcontrol(0)';/>Architecture
Other checkboxes are same as it is. Now I want to display only selected checkboxes from database on another page. So how do I do that? chkcontrol is for limiting checkbox selection limit upto 3. I am getting correct values while editing also.
A little about my code:
I have code that dynamically displays emails with a checkbox next to each email.
<?
foreach($er as $row){ ?>
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="check" checked="checked">
<?
echo $row[email]."<br><br>";
echo "<input name='emailID' id='emailID' type='hidden' value='".$row[emailID]."' />";
} $emailquery->execute(); ?>
I can't seem to come up with a way that deletes the emailID of each email from a specific database table when you uncheck the checkbox. When you re-check the checkbox, I want to insert it back into the database table.
The emails won't go away, because they are stored in a completely different table than the one I want to insert/remove it from.
I know this is kind of a full question, so I will answer any questions you may have. Thank you in advance for all your help!
first, change your input to
<input name="emailcheckbox[]" value="<?php echo htmlspecialchars($row[email]);?>" ...
so, after you post this form back to server you will have
$_POST['emailcheckbox'] == array('checkedemail1', 'checkedemail2'...)
so you will need to delete all e-mails from your table and insert emails from this array, with this you will delete unchecked ones and save only checked ones
You can do this way:
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="[tablename][email]" checked="checked">
Insert Page:
<?
foreach($_POST['emailcheckbox'] as $item)
{
$query = "INSERT INTO ".$item[0]." VALUES(".$item[1].")";
.....
}....
?>
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 />
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.