Checkbox is checked when go to Edit page - PHP HTML - php

I have a situation where I have create a new information after submit a new info, the information is a success into the database. But when I click on the 'Edit' page, all other information is displayed. But for the input type checkbox, the information does not display at all.
<tr>
<td>Transaction</td>
<td>
<div class="checkbox check-default check-success">
<input id="f1" type="checkbox" value="1" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>
<label for="f1"></label>
</div>
</td>
</tr>
I wonder what I am missing. Please help, thanks.

Remove the value=1 in your code.
Use this code below
<input id="f1" type="checkbox" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>

You specified the value of a default input value = 1, removes value = 1
try
<input id="f1" type="checkbox" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>

Related

Show/hide input after click in checkbox

I'm trying to Show/hide my input after check/uncheck a checkbox but I getting this info from DB so, I'm using PHP to add 'checked' attribute to my input.
jQuery code is working, but when I refresh the page, my input doesn't show even with attribute checked "enabled".
PHP code
<label>Send cash?</label>
<input class="reg_cash" type="checkbox" name="reg_cash" value="1"
<?php echo ($configs->reg_cash) ? 'checked' : '' ?>>
<div class="reg_cash_amount">
<label>Amount of cash</label>
<input type="text" name="reg_cash_amount" id="coupon_field"/>
</div>
Jsfiddle: https://jsfiddle.net/qmmg3qwo/
try this to hide/show the input field based on database values.
php code
<label>Send cash?</label>
<input class="reg_cash" type="checkbox" name="reg_cash" value="1"
<?php echo ($configs->reg_cash) ? 'checked' : '' ?>>
<?php if($configs->reg_cash) { ?>
<div class="reg_cash_amount">
<label>Amount of cash</label>
<input type="text" name="reg_cash_amount" id="coupon_field"/>
</div>
<?php } ?>
Jquery code
$(".reg_cash").click(function () {
if ($(this).is(":checked")) {
$(".reg_cash_amount").show();
} else {
$(".reg_cash_amount").hide();
}
});

maintain checkbox which were checked after submit

I know there's "PHP keep checkbox checked after submitting form" on here, but that thread does not solve my problem, because I have multiple checkbox, what I need is when you check a checkbox, this stay checked after submit.
At the moment with this code nothing happens, I tried another way but when I check "id7" checkbox, all the checkbox get checked.
I have to know which checkbox was checked by the id that I give it, but I do not know how.
while ($fila = mysql_fetch_array($rs)) {
echo utf8_encode("
<tr>
<td>
".$fila['title']."
</td>
");?>
<td>
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
</td>
<?php
}
}
?>
First, the value of checkbox is $fila['id'] so when you are checking, use $fila['id'] instead of $fila. Also, when PHP receive array input fields with [] in their names the [] will be removed so that the correct POST variable is $_POST['checklist'].
Try changing this line:
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
to
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist']) && is_array($_POST['checklist']) && in_array($fila['id'], $_POST['checklist'])) echo 'checked="checked"'; ?> />
"name" is like a variable name - by using checklist[] you're defining an array as the variable.
Why not just name each checkbox properly? When the form is submitted, use the contents of $_POST to set each variable into the user's $_SESSION.
If the page is refreshed, use the values from $_SESSION to determine if the checkbox should be ticked. Something like (untested):
<input type="checkbox" name="vehicle" value="Bike"
<?php if (isset($_SESSION['bike_checked']) echo 'checked'; ?>> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"
<?php if (isset($_SESSION['car_checked']) echo 'checked'; ?>> I have a car<br>
<input type="submit" value="Submit">

Use database to check a checkbox

I have a page to view assets with an Edit link. When I click the link it goes to edit_case.php which has a form to edit what elements of the row are in the database as checkboxes. However the boxes do not show them as checked. I have the following code...
// get already checked box values
$repop = "SELECT * FROM case_audit WHERE case_id = $case_id";
$popresults = mysqli_query($dbc, $repop);
$row = mysqli_fetch_array($popresults, MYSQLI_ASSOC);
print_r ($row);
The print_r does show the whole record row from DB. which is either a 1 or 0, checked || not checked.
The form...
<div id="facepics">
<label><input type="checkbox" name="Facial1" value="<?php echo $row['frontrest']; ?>" >Front at Rest </label><br>
<label><input type="checkbox" name="Facial2" value="<?php echo $row['frontbigsmile']; ?>" >Front Big Smille</label><br>
<label><input type="checkbox" name="Facial3" value="<?php echo $row['profile']; ?>" >Profile</label><br>
<label><input type="checkbox" name="Facial4" value="<?php echo $row['subvertex']; ?>" >SubMento Vertex</label><br>
</div>
I know I need to turn the 1's to "checked" just not sure how best to do that.
so basically checked="true" attribute in input creates a checked checkbox.
HTML Code looks like
<input type="checkbox" checked="true">
In your case you can do it like:
<input type="checkbox" name="Facial1" value="frontrest" <?= (intval($row['frontrest']) == 1) ? 'checked' : '';>
Also note that I changed value attribute, with frontrest so that you can identify the checkbox uniquely
EDIT: I have modified the code
<input type="checkbox" name="Facial1" <?=$row['frontrest']==1?'checked':''?>>
I often have the same issue where the browser ignores checked="false" and checks all
so I use
<input type="checkbox" checked>

remember user checked boxes populated from db

I have a form with several check boxes populated from a db
CODE:
// populating Checkboxes from db
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills[]" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST[$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
The problem is when the user select some of these check-boxes and submit the form and get error, the form can not remember his choices and he has to re-select them again.
what can I do to go over this issue?
Thanks in advance
This is not correct:
isset($_POST[$n['eName']]
You should look in the $_POST['skills'] array.
According to your implementation of $n['eName'] the following code might work:
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills['.$n['eName'].']" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST['skills'][$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
ps: Be warned that any quotation in $n['eName'] would most likely break your code - one should take measures for these cases.
Edit:
<form method=post>
<?php
$n['id']='someid';
$n['eName'] = 'test';
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills['.$n['eName'].']" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST['skills'][$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
?>
<input type=submit >
</form>

keeping radio button value after post

HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>

Categories