please click to view imagePHP CODE: this the code getting the value from database and use it to checked/unchecked checkbox.
<?php
$value = FALSE;
foreach ($LOCKPERIOD as $lp)
if ($lpg->pay_code == $lp->pay_code) {
if ($lp->generate_payslip == 1) {
$value = TRUE;
} else {
$value = FALSE;
}
}
?>
<input type="checkbox" id="checkbox" checked="<?php echo $value; ?>" >
Please follow following steps, i have edited your code.
<?php
$value = FALSE;
foreach ($LOCKPERIOD as $lp){
if ($lpg->pay_code == $lp->pay_code) {
if ($lp->generate_payslip == 1) {
$value = TRUE;
} else {
$value = FALSE;
}
}
<input type="checkbox" id="checkbox" <?php echo ($value==TRUE)? 'checked':'';?> >
}
?>
It will work. Kindly try it.
Related
What I need is if I choose first option from a list, which is --------------- and press submit button then they should return a NULL value in my database.
I tried to put this in my code:
if ($_POST['fk_KOMANDAid_KOMANDA'] === '') {
$_POST['fk_KOMANDAid_KOMANDA'] = NULL;
}
But it's not working, my page show me same problem, that fk_KOMANDAid_KOMANDA badly entered.
There is my code:
<p>
<label class="field" for="fk_KOMANDAid_KOMANDA">Komanda<?php echo in_array('fk_KOMANDAid_KOMANDA', $required) ? '<span> *</span>' : ''; ?></label>
<select id="fk_KOMANDAid_KOMANDA" name="fk_KOMANDAid_KOMANDA">
<option value="-1">---------------</option>
<?php
$kom = $asmuoObj->getkomanda();
foreach($kom as $key => $val) {
$selected = "";
if(isset($data['fk_KOMANDAid_KOMANDA']) && $data['fk_KOMANDAid_KOMANDA'] == $val['id']) {
$selected = " selected='selected'";}
echo "<option{$selected} value='{$val['id']}'>{$val['pavadinimas']}</option>";}
?>
</select>
</p>
UPDATE 1
That how my code looks like now, but my page showed me an errors.
<p>
<?php
if ($_POST['fk_KOMANDAid_KOMANDA'] == '-1') {
$_POST['fk_KOMANDAid_KOMANDA'] = NULL;}
?>
<label class="field" for="fk_KOMANDAid_KOMANDA">Komanda<?php echo in_array('fk_KOMANDAid_KOMANDA', $required) ? '<span> *</span>' : ''; ?></label>
<select id="fk_KOMANDAid_KOMANDA" name="fk_KOMANDAid_KOMANDA">
<option value="-1">---------------</option>
<?php
$kom = $asmuoObj->getkomanda();
foreach($kom as $key => $val) {
$selected = "";
if(isset($data['fk_KOMANDAid_KOMANDA']) && $data['fk_KOMANDAid_KOMANDA'] == $val['id']) {
$selected = " selected='selected'";}
echo "<option{$selected} value='{$val['id']}'>{$val['pavadinimas']}</option>";}
?>
</select>
</p>
You have set value -1 to the option.
Two options:
1) Change option value to blank "".
<option value="">---------------</option>
2) At PHP submit side, add if condition
if ($_POST['fk_KOMANDAid_KOMANDA'] == -1) {
$fk_KOMANDAid_KOMANDA = NULL;
}
You are not setting your condition right. You need to put -1, not empty in your if statement. Because when selected ------------ it's value is -1.
if ($_POST['fk_KOMANDAid_KOMANDA'] == '-1') {
$_POST['fk_KOMANDAid_KOMANDA'] = NULL;
}
I have code:
<?php
if ($user['x'] == 1) { $x_checked = ' checked'; } else { $x_checked = ''; }
if ($user['y'] == 1) { $y_checked = ' checked'; } else { $y_checked = ''; }
if ($user['a'] == 1) { $a_checked = ' checked'; } else { $a_checked = ''; }
if ($user['b'] == 1) { $b_checked = ' checked'; } else { $b_checked = ''; }
if ($user['c'] == 1) { $c_checked = ' checked'; } else { $c_checked = ''; }
[...]
?>
<input name="a" type="checkbox"<?php echo $a_checked; ?> />
<input name="b" type="checkbox"<?php echo $b_checked; ?> />
<input name="c" type="checkbox"<?php echo $c_checked; ?> />
[...]
and i have too long code (others same lines). How shortcode to this?
Just check in the input HTML:
<input name="a" type="checkbox" <?php echo ($user['a'] == 1) ? 'checked' : '' ?> />
<input name="b" type="checkbox" <?php echo ($user['b'] == 1) ? 'checked' : '' ?> />
<input name="c" type="checkbox" <?php echo ($user['c'] == 1) ? 'checked' : '' ?> />
If the values can only be 0 or 1 (or maybe more than 1 if you want that checked) then it is shorter:
<?php echo $user['c'] ? 'checked' : '' ?>
If you're going to have a $user element for each checkbox then loop it:
<?php foreach($user as $key => $val) { ?>
<input name="<?php echo $key ?>" type="checkbox" <?php echo $val ? 'checked' : '' ?> />
<?php } ?>
From your comment it appears you may be echoing, if so then just:
foreach($user as $key => $val) {
$checked = $val ? 'checked' : '';
echo '<input name="'.$key.'" type="checkbox" '.$checked.'/>';
}
Welcome to Stackoverflow!
Foreach loops and arrays are in this case your best friends, this is how I usually do it.
<?php
$input_name = array('a', 'b', 'c', 'd');
input_data = '';
foreach ($input_name as $value) {
if ($user[$value] == 1) {
$input_data .= '<input name="'.$value.'" type="checkbox" checked>';
} else {
$input_data .= '<input name="'.$value.'" type="checkbox">';
}
}
?>
Echo the results in the HTML part:
<?=$input_data?>
<?php
$fields = [
'a',
'b',
'etc'
];
foreach ($fields as $field){
if($user[$field] == 1){
$checked = 'checked';
}else{
$checked = '';
}
print('<input name="'.$field.'" type="checkbox" '.$checked.' />');
}
?>
I have a form with multiple checkboxes. I want to allow the user to select at least 1 in each checkbox field, if the user does not check any of the options in the checkbox, a message will display "You have to select at least one." How do I do it? I have here some of my codes:
<div>
<div><span class="font-2">Categories:</span></div>
</div>
<div class = "addinfo">
<?php $categories = array('Breakfast', 'Lunch', 'Dinner', 'Snack', 'Grill', 'Buffet', 'Fast Food');
$values = explode(',' , $row['categories']);
?>
<?php foreach($categories as $category) {
$cat='';
foreach($values as $value){
if($value == $category){
$cat ="checked";
}
?>
<input <?php echo $cat ?> type="checkbox" name="category[]" value="<?php echo $category?>"><?php echo $category?><br>
<?php }
}?>
</div>
form
<form method = "POST" action = "<?php echo base_url().'/index.php/AdminController/operation'?>">
AdminController
public function addResto(){
$this->load->model('AdminModel');
$this->AdminModel->insert();
$this->getRestos();
}
public function operation(){
if(isset($_POST['btn'])){
if(empty($_POST['id'])){
$this->addResto();
}
else{
$this->updatingResto();
}
}
}
public function updateResto($id){
$this->load->model('AdminModel');
$restaurantinfo['restaurantinfo']=$this->AdminModel->getResto($id);
$this->load->view('admin/UpdateRestoPage',$restaurantinfo);
}
public function updatePage(){
$this->load->view('admin/UpdateRestoPage');
}
public function updatingResto(){
$id = $_POST['id'];
$this->load->model('AdminModel');
$this->AdminModel->update($id);
}
You should also check if there is a checkbox that is checked
public function operation(){
if(isset($_POST['btn'])){
if(empty($_POST['category'])){
$this->load->view('YOUR_VIEW_FILE',array('error'=>'category'));
}else if(empty($_POST['id'])){
$this->addResto();
}
else{
$this->updatingResto();
}
}
}
your view file add a condition
<?php
if(!empty($error) && $error=="category"){
?>
<script>alert("You have to select at least one.");</script>
<?php
}
?>
Add validation in your called php
<?php
$checked = false;
foreach($_POST["category"] as $key) {
if(!empty($key)) {
$checked = true;
}
}
if(!$checked) {
echo "<script>";
echo "alert('You have to select at least one.');";
echo "history.back();";
echo "</script>";
exit();
}
?>
in your controllers operation function use
if (isset($_POST['mycheckbox'])) {
echo "checked!";
} esle {
//send again with error message
}
Using this validation in javascript:
$("input:checkbox[name='a']:checked").length == 0
Example:
HTML code:
<from name='frm1' method='' action='POST'>
<label>Favorites:</label>
<span>sports</span><input type="checkbox" name="fav" value="1"/>
<span>music</span> <input type="checkbox" name="fav" value="2"/>
<span>reading</span> <input type="checkbox" name="fav" value="3"/>
<input type="button" value="validate">
</form>
Javascript code:
function validate() {
//replace with your own code here
if ($('input:checkbox[name="fav"]:checked').length == 0) {
alert("You have to select at least one.");
}
}
$("input[type='button']").click(validate)
It's just a simple example to demonstrate the use of :check selector(JQuery), you need to
change and embed it in your project!
try to add this in your html code:
<script type="text/javascript">
function check () {
var inputs = document.querySelectorAll("input[type='checkbox']");
for(var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
return true;
}
}
alert("You have to select at least one.");
return false;
}
</script>
This one will show the error before the page refreshes. And add something like this in your html :
<input type = "button" onclick = "check();">
UPDATE : try to replace your code with this.
<div>
<div><span class="font-2">Categories:</span></div>
</div>
<script type="text/javascript">
function check () {
var inputs = document.querySelectorAll("input[type='checkbox']");
for(var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
return true;
}
}
alert("You have to select at least one.");
return false;
}
</script>
<div class = "addinfo">
<?php $categories = array('Breakfast', 'Lunch', 'Dinner', 'Snack', 'Grill', 'Buffet', 'Fast Food');
$values = explode(',' , $row['categories']);
?>
<?php foreach($categories as $category) {
$cat='';
foreach($values as $value){
if($value == $category){
$cat ="checked";
}
?>
<input <?php echo $cat ?> type="checkbox" name="category[]" value="<?php echo $category?>"><?php echo $category?><br>
<?php }
}?>
<input type = "button" onclick = "check();">
</div>
This is my code :
<td>
<?php
if ($adPropertyPayment == "Direct") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
if ($adPropertyPayment == "CPC") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
if ($adPropertyPayment == "CPM") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
?>
<input type="radio" id="radioPaymentDirect" name="payment" value="Direct" <?php echo $checked ?> onclick="showAmount('Direct');" />Direct
<input type="radio" id="radioPaymentCPC" name="payment" value="CPC" <?php echo $checked ?> onclick="showAmount('CPC');" />CPC
<input type="radio" id="radioPaymentCPM" name="payment" value="CPM" <?php echo $checked ?> onclick="showAmount('CPM');" />CPM
</td>
Checked is not working. I am getting the value of $aspropertypayment in POST.
Use this
<?php
$adPropertyPayment = $_POST['payment'];
if ($adPropertyPayment == "Direct") {
$checkedDir = "checked = 'checked'";
} else {
$checkedDir = "";
}
if ($adPropertyPayment == "CPC") {
$checkedCpc = "checked = 'checked'";
} else {
$checkedCpc = "";
}
if ($adPropertyPayment == "CPM") {
$checkedCpm = "checked = 'checked'";
} else {
$checkedCpm = "";
}
?>
<input type="radio" id="radioPaymentDirect" name="payment" value="Direct" <?php echo $checkedDir;?> onclick="showAmount('Direct');" />Direct
<input type="radio" id="radioPaymentCPC" name="payment" value="CPC" <?php echo $checkedCpc;?> onclick="showAmount('CPC');" />CPC
<input type="radio" id="radioPaymentCPM" name="payment" value="CPM" <?php echo $checkedCpm;?> onclick="showAmount('CPM');" />CPM
Try writting only $checked = 'checked'; in your if statement.
I want to apply validation on multiple check boxes but question is how to do it?
<?php
mysql_connect("localhost","root","thisis");
mysql_select_db("my_database");
if(isset($_GET["q"]))
{
$my_q = $_GET['q'];
$q="select * from subjects where subj_code='$my_q'";
$rs=mysql_query($q);
for($i=0;$i<mysql_num_rows($rs);$i++)
{
$rd=mysql_fetch_object($rs);
?>
<input type="checkbox" name="subj[]" value="<?php echo $rd->subj_name; ?>" /><?php echo $rd->subj_name; ?>
<br />
<?php
}
}
?>
Try the below code.
obj = document.form1.elements("subj[]");
for (i = 0; i < obj.length; i++) {
if (obj[i].checked) {
alert("Your text");
return true;
}
}
Try This:
if($('input[name="sub[]"]:checked').length == 0) {
alert('No checkbox is checked');
return false;
}