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>
qwertyuiop
  • 43
  • 10
  • Hello, Welcome to SO! Please take the [tour](https://stackoverflow.com/tour) and read the [help] 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? – NcXNaV Jul 23 '21 at 09: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! – qwertyuiop Jul 23 '21 at 09: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. – NcXNaV Jul 23 '21 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. – NcXNaV Jul 23 '21 at 10:58
  • Yep changed that, but now it displays all checkboxes, not sure why that is. Could it be `value="`. Another issue is when I load into the form the checkboxes its only after I press submit that it shows? – qwertyuiop Jul 23 '21 at 11:19
  • Another problem is the `name` attribute. You should use array like `name="toping[]"`, that way you can know which selections are selected among those 7 checkboxes. Check [this](https://stackoverflow.com/questions/4631224/getting-multiple-checkboxes-names-ids-with-php) out: multiple checkboxes with the same name. – NcXNaV Jul 23 '21 at 11:26
  • I have read the link attached but I am still a bit new to PHP and not to sure how to implement this, if you have any ideas that would be helpful. Thanks for helping! – qwertyuiop Jul 23 '21 at 12:44
  • Okay just to clarify, do you expect one Topping only or user can also check multiple toppings? Suppose users can select multiple Toppings `checkboxes`, what is the format for `toping` column in DB? `Vanilla, Caramel, Oreo` separated with comma `,` like this or how ? – NcXNaV Jul 23 '21 at 16:50

1 Answers1

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:

NcXNaV
  • 1,657
  • 4
  • 14
  • 23
  • I believe here you are using `PHP PDO` is it possible to make this possible for a program such as `PgAdmin`? – qwertyuiop Jul 25 '21 at 06:02
  • Do you mean using pdo to connect with pgsql? – NcXNaV Jul 25 '21 at 06:24
  • Yes exactly! I am not to sure how to do that – qwertyuiop Jul 25 '21 at 06:27
  • Okay I will add the php connection string for `PostgreSQL` database. – NcXNaV Jul 25 '21 at 06:46
  • I've added a sample connection string you can use. That's what I used with XAMPP and MySQL. Check [this](https://www.phptutorial.net/php-pdo/pdo-connecting-to-postgresql/) out. – NcXNaV Jul 25 '21 at 07:15
  • Is there a way to do this without PDO – qwertyuiop Jul 28 '21 at 04:47
  • Yes of course, you can also use `pg` to connect, it does not matter. What you need to change is the connection strings, and how you fetch the data. I've added a sample, I copied the DB details from your other post. Let me know if that works. – NcXNaV Jul 28 '21 at 05:29
  • Warning: Array to string conversion in /Applications/mappstack-8.0.7-0/apache2/htdocs/cafe/update-process.php on line 8 Warning: pg_query(): Query failed: ERROR: syntax error at or near "]" LINE 5: colour='#000000',toping[]='Array', chocosentence='LOL, I MEA... ^ in /Applications/mappstack-8.0.7-0/apache2/htdocs/cafe/update-process.php on line 10 Warning: Undefined array key "toppings" in /Applications/mappstack-8.0.7-0/apache2/htdocs/cafe/update-process.php on line 16 I am getting these errors `toping[]='$_POST[toping]'` --> also is this right – qwertyuiop Jul 28 '21 at 07:25
  • Do I have to change anything in the table – qwertyuiop Jul 28 '21 at 07:30
  • Wait, which `sql` query did you try that returns the error? if toping is a php array, then assign it `toping = $_POST['toping']`. – NcXNaV Jul 28 '21 at 07:31
  • okay that seemed to fix most of the errors but now its coming back with Warning: Array to string conversion in /Applications/mappstack-8.0.7-0/apache2/htdocs/cafe/update-process.php on line 8. Does that means its aving trouble converting it from an array to string, if so how do i remedy this – qwertyuiop Jul 28 '21 at 08:43
  • Can you copy the code on `line 8`, is that a `pg_query()` statement? If you're using `toping` as value it won't work since it's an array. I've included a reference to store multiple checkbox value, check it [here](How to Insert Multiple Checkbox Value to Database). Basically use `implode` to make it **comma-separated strings` then use it to save it in DB – NcXNaV Jul 28 '21 at 08:45
  • `colour='$_POST[colour]',toping='$_POST[toping]', chocosentence='$_POST[chocosentence]', ` – qwertyuiop Jul 28 '21 at 09:01
  • Your `' '` (quote) should be inside the $_POST `[ ]` bracket. And you need to add `$` symbol to colour, toping, chocosntence as it's `php` variable. So it should be `$colour=$_POST['colour']`. – NcXNaV Jul 28 '21 at 09:05
  • Everything is good now, thanks so much for your help, really appreiciate it! – qwertyuiop Jul 29 '21 at 01:28
  • Your Welcome! I'd still recommend using prepared statement with `PDO` or `pg_prepare` to prevent injection. Take a look at this: [pg-prepare](https://www.php.net/manual/en/function.pg-prepare.php). – NcXNaV Jul 29 '21 at 02:33