3

I have to show a checked check box (like this one), but currently when extracting the value from the database (it shows this)

Is there a way to show the checked check box when extracting the known value from the database (PgAdmin)(All code is either HTML,CSS or PHP)

Code:THIS IS THE TABLE DATA

 Topings: <br>
 <br>
 <label class="checkbox-container">
   <input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Vanilla") echo "checked" ?> value="<?php echo $row['toping']; ?>">
   <span class="checkmark"></span>
   <span class="checkbox-txt"> Vanilla</span>
   <br /></br>
 </label>
 <label class="checkbox-container">
   <input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Chocolate") echo "checked" ?>value="<?php echo $row['toping']; ?>">
   <span class="checkmark"></span>
   <span class="checkbox-txt">Chocolate</span>
   <br /></br>
 </label>
 <label class="checkbox-container">
   <input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Caramel") echo "checked" ?>value="<?php echo $row['toping']; ?>">
   <span class="checkmark"></span>
   <span class="checkbox-txt">Caramel</span>
   <br /></br>
 </label>
 <label class="checkbox-container">
   <input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Strawberry") echo "checked" ?>value="<?php echo $row['toping']; ?>">
   <span class="checkmark"></span>
   <span class="checkbox-txt">Strawberry</span>
   <br /></br>
 </label>
 <label class="checkbox-container">
   <input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="M&M's") echo "checked" ?>value="<?php echo $row['toping']; ?>">
   <span class="checkmark"></span>
   <span class="checkbox-txt">M&M's</span>
   <br /></br>
 </label>
 <label class="checkbox-container">
   <input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Oreo") echo "checked" ?>value="<?php echo $row['toping']; ?>">
   <span class="checkmark"></span>
   <span class="checkbox-txt">Oreo</span>
   <br /></br>
 </label>
 <label class="checkbox-container">
   <input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping']=="Meringue") echo "checked" ?>value="<?php echo $row['toping']; ?>">
   <span class="checkmark "></span>
   <span class="checkbox-txt">Meringue</span>
   <br />
 </label>
8
  • Hello, Welcome to SO! Please take the tour and read the help center page. For your question, what do you get from var_dump($_POST)? Have you confirmed that your $_POST variable contain the right value? Why do you have to echo checked on value attribute? Commented Jul 23, 2021 at 9:21
  • Thanks @NcXNaV, The 'echo checked on value attribute' was a mistake I have gotten rid of that now but to answer the other part the var_dump($_POST) the results I got were: ["toping"]=> string(182) “Vanilla This is the correct value, Hopefully this helps! Commented Jul 23, 2021 at 9:56
  • I copied and tested your code on php and set $_POST['toping'] = 'Vanilla', it worked and it should, The Vanilla checkbox is checked. Try to add a space between ?> and value, and let me know if that solves the problem. Commented Jul 23, 2021 at 10:50
  • Remember to add the space: echo "checked" ?> value= instead of echo "checked" ?>value. When I run your code, only Vanilla checkbox works, other than that it isn't, be careful. Commented Jul 23, 2021 at 10:58
  • Yep changed that, but now it displays all checkboxes, not sure why that is. Could it be value="<?php echo $row['toping']; ?>. Another issue is when I load into the form the checkboxes its only after I press submit that it shows? Commented Jul 23, 2021 at 11:19

1 Answer 1

2

To begin with, you have to know how to store multiple checkbox values to database. I've added a reference if you need it.

Assuming your toppings is saved as comma-separated value like this:

Comma-separated-toppings

  1. You can use explode() function to explode comma-separated value to a flat, indexed array.

    From Chocolate,Caramel,M&M's to ["Chocolate", "Caramel", "M&M's"].

  2. Then you can use in_array() function to check if the item(topping) is available or not in array.

PHP

/* Connection with PDO */
// DB details (Change it to your DB details)
$host = '127.0.0.1';
$user = 'root';
$password = 'root';
$dbname = 'test';
$port = '3306';

// DataSourceName for MySQL (The one I used for testing)
$dsn = "mysql:dbname=$dbname;host=$host;port=$port";
// DataSourceName for PostgreSQL (Use this and add port if you need)
$dsn = 'pgsql:dbname=$dbname;host=$host;';

$pdo = new PDO($dsn, $user, $password);
/* End Connection String */

// Get Toppings Value From DB
$statement = $pdo->query("SELECT `toppings` FROM toppings");
$res = $statement->fetch();

//res[0] = Comma Separated String of Toppings: 'Topping1,Topping2,Topping3'
$toppings = explode(',', $res[0]);
// $toppings = Array of toppings checked: ["Topping1", "Topping2", "Topping3" ]


/* Connection with PG */
$db = pg_connect("host=localhost port=5432 dbname=SRIS user=postgres password=password");
$result = pg_query($db, "SELECT * FROM tablename ");
$row = pg_fetch_assoc($result);

$toppings = explode(',', $row['toppings']);

HTML FORM

<label class="checkbox-container">
  <input type="checkbox" name="toping[]" <?=(in_array('Vanilla',$toppings) ? 'checked="checked"' : '')?> value="Vanilla">
  <span class="checkmark"></span>
  <span class="checkbox-txt"> Vanilla</span><br/></br>
</label>
<label class="checkbox-container">
    <input type="checkbox" name="toping[]" <?=(in_array('Chocolate',$toppings) ? 'checked="checked"' : '')?> value="Chocolate">
    <span class="checkmark"></span>
    <span class="checkbox-txt">Chocolate</span><br/></br>
</label>
<label class="checkbox-container">
    <input type="checkbox" name="toping[]" <?=(in_array('Caramel',$toppings) ? 'checked="checked"' : '')?> value="Caramel">
    <span class="checkmark"></span>
    <span class="checkbox-txt">Caramel</span><br/></br>
</label>
<label class="checkbox-container">
    <input type="checkbox" name="toping[]" <?=(in_array('Strawberry',$toppings) ? 'checked="checked"' : '')?> value="Strawberry">
    <span class="checkmark"></span>
    <span class="checkbox-txt">Strawberry</span><br/></br>
</label>
<label class="checkbox-container">
    <input type="checkbox" name="toping[]" <?=(in_array("M&M's",$toppings) ? 'checked="checked"' : '')?> value="M&M's">
    <span class="checkmark"></span>
    <span class="checkbox-txt">M&M's</span><br/></br>
</label>
<label class="checkbox-container">
    <input type="checkbox" name="toping[]" <?=(in_array('Oreo',$toppings) ? 'checked="checked"' : '')?> value="Oreo">
    <span class="checkmark"></span>
    <span class="checkbox-txt">Oreo</span><br/></br>
</label>
<label class="checkbox-container">
    <input type="checkbox" name="toping[]" <?=(in_array('Meringue',$toppings) ? 'checked="checked"' : '')?> value="Meringue">
    <span class="checkmark "></span>
    <span class="checkbox-txt">Meringue</span><br/>
</label>

Note:

  • I've changed the name attribute to name=topping[], you should use field name array.
  • I've also changed the value attribute, giving a predetermined value to each checkbox.

References:

Sign up to request clarification or add additional context in comments.

16 Comments

I believe here you are using PHP PDO is it possible to make this possible for a program such as PgAdmin?
Do you mean using pdo to connect with pgsql?
Yes exactly! I am not to sure how to do that
Okay I will add the php connection string for PostgreSQL database.
I've added a sample connection string you can use. That's what I used with XAMPP and MySQL. Check this out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.