PHP Checkbox Set to Check Based on Database Value

PHP checkbox set to check based on database value

Add this code inside your input tag

<?php if ($tag_1 == 'yes') echo "checked='checked'"; ?>

Laravel set checkboxes checked based on database value

This is how I resolved it, I added foreach and if statement in the input to check if it the same value as from database:

             <div class="form-group" id="ageCheckboxes">
@foreach ($child_age_groups as $age)
<div class="checkbox checkbox-info checkbox-inline">
<input class="age_group_checkbox" type="checkbox" value="{{$age->id}}" name="age_group[]" id="age_group{{$age->id}}" @foreach ($nanny_babysitting_ages as $ages) @if($age->id == $ages->ages ) checked @endif @endforeach />
<label for="age_group{{$age->id}}">{{$age->age_group}}</label>
</div>
@endforeach
</div>

How to make Multiple PHP checkbox set to checked based on database result?

You can store your values in to an array then you can use in_array() to check:

<?php
$interest = array(); // initializing
while($row = $result->fetch_assoc())
{
$interest[] = $row['interest_name']; // store in an array
}
?>

HTML:

<td>
<input type = "checkbox" name = "checkbox[]" value = "Badminton" <?=(in_array('Badminton',$interest) ? 'checked="checked"' : '')?> />Badminton
</td>

Side Note: Change other checkbox as same above with different values which you have.

How to Make Checkbox Status checked or unchecked based database values?

Try

if($row->IsActive==1)
{
echo '<td><input type="checkbox" checked="checked" name="status" id="status"/></td>';
}
else
{
echo '<td><input type="checkbox" name="status" id="status"/></td>;
}

Show checkbox values from database in PHP

Try this:

$lang = $pdo->prepare("SELECT `language` FROM admin_panel_languages WHERE user_id=:user_id");
$lang->execute(array(":user_id"=>$user_id));
$lang_spoken=$lang->fetchAll(PDO::FETCH_ASSOC);
print_r($lang_spoken);

$checkedEng = '';
$checkedHindi = '';

foreach($lang_spoken as $lang) {
if (($lang['language'] == "English")) {
$checkedEng = 'checked';
} else if ($lang['language'] == "Hindi") {
$checkedHindi = 'checked';
}
}

<label class="col-md-4">
<input type="checkbox" value="English" name="language[]" id='checkboxes' <?php echo $checkedEng; ?>/> English </label>

<label class="col-md-4">
<input type="checkbox" value="Hindi" name="language[]" id='checkboxes' <?php echo $checkedHindi; ?>/> Hindi</label>

?>

Though I haven't tested the above code, but I think this one should work for you.

Set checked checkbox based on database

I'm assuming you saved in the table business_service 1 check per row, so right now, your code is printing all the checkboxes as many times as items selected, you should store the checkboxes in an array, and then check the array

<?php 
$conn=new PDO( "mysql:host=$servername;dbname=$dbname",

$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql
$conn->prepare("SELECT bs_services FROM business_service WHERE bs_center = :email AND bs_package = 'Essential'"); $sql->bindParam(':email', $email, PDO::PARAM_STR); $sql->execute(); $rows = $sql->fetchAll();
foreach ($rows as $row) {
$checks[$row[ 'bs_services']] = true;
}
?>
<button type="button" class="accordion"><b>Engine</b>
</button>
<div class="panel-accordion" style="max-height: auto;">
<div class="row">

<li class="checkbox">
<input type="checkbox" id="amenity-1" name="service[]" value="Up to 4.5 L standard oil change" <?php if(isset($checks["Up to 4.5 L standard oil change"])) echo "checked"; ?>>
<label for="amenity-1">Up to 4.5 L standard oil change</label>
</li>

<li class="checkbox">
<input type="checkbox" id="amenity-2" name="service[]" value="Up to 6.0 L standard oil change" <?php if(isset($checks["Up to 6.0 L standard oil change"])) echo "checked"; ?>>
<label for="amenity-2">Up to 6.0 L standard oil change</label>
</li>

<li class="checkbox">
<input type="checkbox" id="amenity-3" name="service[]" value="Up to 6.0 L high quality change" <?php if(isset($checks["Up to 6.0 L high quality change"])) echo "checked"; ?>>
<label for="amenity-3">Up to 6.0 L high quality change</label>
</li>

<li class="checkbox">
<input type="checkbox" id="amenity-4" name="service[]" value="Replace oil filter up to $20" <?php if(isset($checks["Replace oil filter up to $20"])) echo "checked"; ?>>
<label for="amenity-4">Replace oil filter up to $20</label>
</li>

<li class="checkbox">
<input type="checkbox" id="amenity-5" name="service[]" value="Replace oil filter up to $40" <?php if(isset($checks["Replace oil filter up to $40"])) echo "checked"; ?>>
<label for="amenity-5">Replace oil filter up to $40</label>
</li>

<li class="checkbox">
<input type="checkbox" id="amenity-6" name="service[]" value="Carry out engine diagnotistic scan" <?php if(isset($checks["Carry out engine diagnotistic scan"])) echo "checked"; ?>>
<label for="amenity-6">Carry out engine diagnotistic scan</label>
</li>

</div>

Comparing checked checkbox values with values in DB

Thanks that helped me understand better what you are trying to do. Looks to me that your main issue in your logic is that you can't identify the col name you need to query because you not passing that data. Try something like this:

<form action="function.php" method="post">
<input type="checkbox" name = "checkbox[car_name]" value="<?php echo $_SESSION['car_name']; ?>"> Car name<br>
<input type="checkbox" name = "checkbox[wheel_size]" value="<?php echo $_SESSION['wheel_size']; ?>"> Wheel size<br>
<input type="checkbox" name = "checkbox[year]" value="<?php echo $_SESSION['year']; ?>"> YEAR of PROD<br>
<input type="checkbox" name = "checkbox[max_speed]" value="<?php echo $_SESSION['max_speed']; ?>"> MAX SPEED<br>

<button>Submit CHOICES</button>
</form>

Once you have the col name inside your checkbox array, you can use that so you don't need to manually do the query for every possibility.

<?php
// The $_POST['checkbox'] array needs to survive the sanitizing part
foreach($_SESSION['checkbox'] as $col=>$value){

$sql="SELECT * FROM ecars WHERE $col = '$value'";
$col_title = str_replace('_', ' ', $col);

if ($result=mysqli_query($conn,$sql)){
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
$vars = array($col_title, $rowcount);
printf("Number of %d which have same VALUE as you entered: %d.\n",$vars);
echo "<br>";
// Free result set
mysqli_free_result($result);
}

}
?>

Let me know how that works out

Checkbox auto checked based on value from database

it should be like this

<input type="checkbox" name="CarModel" value="Yes"  <?php echo ($row['CarModel'] == 'Audi' ) ? 'checked' : NULL ; ?> >

How could I display a checked checkbox based from it's value in the database?

<?php

$sql = "SELECT somecol FROM sometable";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$checked = $result['somecol'];

?>

<input type="checkbox" name="somecol" value="1" <?php if ($checked == 1) echo 'checked'; ?> />


Related Topics



Leave a reply



Submit