Hello I'm having some problem.
I want it to be checked by default, and if you unselect it and press submit I want it to be unchecked
<input type="checkbox" name="show_signature" value="1"<?php echo isset($_POST['show_signature'])) ? ' checked=""' : '' ?>>
This works good unchecking > submitting and checkbox is unchecked and same if you check it and send the form, it stays checked.
But, I want it to be checked by default. Should'nt this work?
if (isset($_POST['show_signature'])) {
echo ' checked=""';
} else {
echo '';
}
Tried this to
if (isset($_POST['show_signature']) || !isset($_POST['show_signature'])) {
echo ' checked=""';
} else {
echo '';
}
Okay; from the question, I got this process
When the page loads the first time, you want the checkbox to be checked.
When the page is submitted, if the checkbox is unchecked, let it remain unchecked; otherwise, let it be checked.
Try
/*making the assumption that the submission process starts when a submit button
with name **submit** is present, use this
*/
if (isset($_POST["submit"])){
value = '<input type="checkbox" name="show_signature"';
value .= (isset($_POST["show_signature"]))? 'checked="checked"': "";
value .= ' />';
print value;
}
else{ //when the page is initially loaded
print '<input type="checkbox" name="show_signature" checked="checked" />';
}
The reason the post depends on the submit button (or any other field)t is because if the user unchecks the box, the $_POST["show_signature"] variable will not be found, and the form will not be processed at all.
That should resolve the issue.
Hope the explanation is clear and this helps.
Related
Looking for some help with checkbox(switch type) that I have put in PHP.
These checkbox are created dynamically with data received from a MySQL Database.
The checkbox is loaded on the page depending on the condition of the checkbox placed on the database.
Now, I need help to build a code that perform some action like writing back into another database table everytime this checkbox is actioned; i.e. its inital state can be :checked or left blank(unchecked) and each of these checkbox have an ID or value on them from database. But any action that takes place either from blank to :checked or from :checked to blank, it needs to trigger an action for that particular actioned checkbox only.
Would need help on this as PHP please.
My Code Looks like this for a checkbox:
echo "<input type='checkbox' class='input-swtoggle' name='switch-box' ";
$lightStatus = $row->light_status;
if ($lightStatus == "1") {
echo "checked>";
}
else{
echo ">";
}
PS: I did try alot Googling and on Stackoverflow, but none of them are helping for the above condition.
You can bind click event to the checkbox using .on()
$(function(){
$(document).on('click',"input:checkbox.input-swtoggle",function() {
alert($(this).is(':checked'));
});
});
<?php
// HTML code
$checkced = ($row->light_status == '1') ? 'checked="checked"' : '';
echo '<input type="checkbox" class="input-swtoggle" name="switchbox[]"' . $checkced . '/>';
// Code after form posted:
if (! empty($_POST['switchbox'])) {
foreach ($_POST['switchbox'] as $switch) {
// Apply your logic here.
}
}
?>
Intro
I have some custom fields in my WordPress profile area. Here's an example of a checkbox in my code:
<input type="checkbox" name="A" id="A" value="yes" <?php if (esc_attr( get_the_author_meta( "A", $user->ID )) == "yes") echo "checked"; ?> /><label for="my_field "><?php _e("Order Button"); ?></label><br />
This field is A, and I'm saving it in the same file with:
update_usermeta( $user_id, 'A', $_POST['A'] );
This is working, and when the user clicks it on or off, it saves.
The Problem
However, I am having a big issue posting the results if the checkbox was checked.
In another file I have this:
if(isset($_POST['A'])) {
echo 'asd';
} else { }
I have tried a lot of variations, but I can not figure out how to get the If statement to check whether the user checked the box or not, it only echos the else.
TL;DR:
I can't figure out how to check if the usermeta is checked or not, and how to output that into another element of the website.
http://pastebin.com/Q3WSv47m Link to User Profile Code
http://pastebin.com/spU2dqGk Link to Header area where I'm trying to call the checkbox.
You must check checked variable value. Try this:
if (isset($_POST['A']) && $_POST['A'] == 'yes')
{
echo 'A is checked';
}
else
{
echo 'Something else is checked.';
}
note: make sure your form method is POST.
I've tried to get those checkbox that are unchecked. At this moment i'm just getting those that are checked. Here is my code. That value will be inserted on a table which i don't want to leave it null, that's why i need to get those checkbox unchecked, so that i could insert 1 for checked or 0 for unchecked:
<?php
if (!empty($_POST['menu'])) {
# code...
foreach ($_POST['menu'] as $value) {
# code...
echo "<p>ID Checkbox checked: ".$value;
}
}
?>
One of the reasons for what i need to get both status: checked or unchecked is because i don't want to leave database fields empty.
Unchecked checkboxes don't get POSTed. If you know what fields should be there, you'll probably have to list them out manually.
The wonderful thing about checkboxes is that when you don't have one checked, and you submit a form, nothing is sent to the server. Take this checkbox for example:
<input type="checkbox" name="test"/>
If you left it unchecked and looked for $_POST['test'] it would error out, as it is not set.
So, try doing this:
if(!isset($_POST['name_of_checkbox'])){
// Fill database with some empty value.
} else {
// Do what you need to do for checked value.
}
Hope that gives you some insight!
Say if they are named the same, and you know the number of checkbox you can use a for loop:
if (!empty($_POST['menu'])) {
for ($i=0; $i < 10; $i++) {
if(isset($_POST['menu'][$i])){
echo "Checkbox checked: " . $_POST['menu'][$i];
}else{
echo "Checbox uncheck #" . $i;
}
}
}
You can put the names in an array, then iterate:
$checkboxes = array('cbx1', 'cbx2', 'cbx3');
foreach ($checkboxes as $checkbox) {
if (isset($_POST[$checkbox])) {
echo "<p>ID Checkbox checked: " . $_POST[$checkbox];
} else {
echo "Checbox uncheck :" . $checkbox;
}
}
So yeah many ways to achieve this, it depends on the situation.
Check for #Artur approach as well for client side solution.
<input type="hidden" name="checkbox[8]" value="0">
<input type="checkbox" name="checkbox[8]" value="8">
This would be one way to also get 0's posted. Important part is that the name's are the same. So if checkbox is checked, it's value would override the hidden input value.
Thank you all for your time and be so kind to answer my question. The reason for what i needed to have those checkboxes unchecked and checked is because i have a dynamic menu and so i assign menus to users. Here is the solution i found with a friend of mine at work:
Code for the checkboxes. I query all the menus available on my table Menus so that i will show the administrator all the menus availables so then he could choose which menus he will assign to the users:
´
<table>
<tr>
<td>Menus a asignar:</td>
<td>
<?php
$query_menus_checkbox = mysql_query("SELECT id_menu, nombre_menu FROM menus");
while ($checkbox_mostrar = mysql_fetch_row($query_menus_checkbox)) {
# code...
?>
<input type="checkbox" name="menus[<?php echo $checkbox_mostrar[0]; ?>]" value="<?php echo $checkbox_mostrar[0] ?>"><?php echo $checkbox_mostrar[1] ?>
<p></p>
<?php
}
?>
</td>
</tr>
</table>
´
Then here is the code to process which checkboxes are checked or not to insert on my table (id_user is not shown but i have taken from another query which is not shown so you'll have to query yourself):
´
$res=mysql_query("select id_menu from menus");
$arid=array();
while($xd=mysql_fetch_assoc($res)){
$arid[]=$xd['id_menu'];
}
for($i=0;$i<count($arid);$i++){
$id_menu=$arid[$i];
$activo=(isset($_POST['menus'][$id_menu]) && $_POST['menus'][$id_menu]!="")?1:0;
$inserta_menus = mysql_query("INSERT INTO menus_usuarios(id_menu, id_usuario, estado) values ('$id_menu', '$id_user[0]', '$activo')");
}
´
I have a form with checkboxes that users fill out and submit. I then have another form that allows them to edit the information they submitted. This second form pre-populates with the information they submitted on the first form. This is fine with text fields, but how do I pre-populate the checkboxes? I.e. if they checked a checkbox on the first form, how do I get the second form to recognise that and display a checked checkbox?
I'm new to Php so sorry I can't be more technical with this query!
Thanks
Luke
When you submit the form, you have the data on the next page. So if theres a checkbox looking like this:
<input type="checkbox" name="over18" value="1" />
You have to check if the person selected the "over 18"-field.
if ($_POST['over18'] == '1') {
$checked = 'checked="checked"';
}
else {
$checked = ''; //If it's not checked
}
Then your output for the checkbox looks like this:
echo '<input type="checkbox" name="over18" value="1" ' . $is_checked .'/>';
So everytime the Checkbox is checked, the next page checks the Checkbox too.
I have a php function script that is supposed to uncheck a checkbox or checkboxes if a user unchecks it when the preview button is clicked but I can only get the last
checkbox that was unchecked to stay unchecked but not the other checkeboxes how can I fix this so that all the checkboxes that where unchecked stay unchecked?
Here is part of my PHP function that is giving me the problem.
if(isset($_POST['preview'])){
foreach($query_cat_id as $qci) {
if(!in_array($qci, $cat_id)){
$unchecked = $purifier->purify(strip_tags($qci));
}
}
}
for ($x = 0; $x < count($query_cat_id); $x++){
if(($query_cat_id[$x] == $cat['id']) && ($cat['id'] != $delete_id) && ($cat['id'] != $unchecked)){
echo 'checked="checked"';
}
}
Why not just just check if the variable is set, if the checkbox is checked when the form is submitted, it will be available in $_POST['checkboxName'], otherwise, isset($_POST['checkboxName']) will return false.
Basic script to test it
<?php
if (isset($_POST['heh']))
echo $_POST['heh'];
else
echo "Not checked";
?>
<form action='yourPage.php' method='post'>
<input type='checkbox' name='heh' />
<input type='submit' />
</form>
View it in action
http://robertsquared.com/so.php
i solve this problem on client side
i have a input of type hidden with the same name as the checkbox with the value of 0 and the checkbox right after the hidden field with the value of 1
if the checkbox is not checked i get the value of 0 from the hidden field and if someone checks i got the 1 from the checkbox.
so i only need to check if $value==1 then checked=checked