Hi,
I'm creating a basic CMS using PHP and MySQL and i'm struggling to get the checkbox information from my html page across into the database. I want the values to appear as binary 0 or 1 values, any help would be greatly appreciated thanks

html document is written as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Create your news page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<fieldset>
<legend>Checked components will show in the page</legend>
<form method="POST" action="">
<span class="label">Header</span>
<input type="checkbox" name="header" value="HEADER">
<br>
<span class="label">Footer</span>
<input type="checkbox" name="footer" value="FOOTER">

<hr>
<span class="label">Local news</span>
<input type="checkbox" name="local" value="LOCALNEWS">
<br>
<span class="label">National news</span>
<input type="checkbox" name="national" value="NATIONALNEWS">
<br>
<span class="label">International news</span>
<input type="checkbox" name="international" value="INTERNATIONALNEWS">
<p>
<input type="submit">
</form>
</fieldset>
</body>
</html>

and php document is written as follows:

<?php
$user="user_***";
$password="*********";
$database="dbxyz";
mysql_connect("localhost", $user, $password);
mysql_select_db($database, $db_handle);
mysql_select_db("dbxyz");
if(isset($_POST))
{
foreach($_POST as $value {
$insert="INSERT INTO layout (header, footer, local, national, international) VALUES ('$value')";
mysql_query($insert);
}
}
?>

Dani AI

Generated

A few practical points that pick up on and and fix the original mistakes: unchecked checkboxes are not sent in $POST (as noted), so you must map the presence/absence of each expected name to 1 or 0 on the server. Prefer storing them as numeric columns (TINYINT(1)) and use parameterised queries (PDO or mysqli) instead of the old `mysql*calls. Use<label>` for better UX as suggested.

Example table (one row per layout option):

CREATE TABLE layout (
  id INT PRIMARY KEY AUTO_INCREMENT,
  header TINYINT(1) NOT NULL DEFAULT 0,
  footer TINYINT(1) NOT NULL DEFAULT 0,
  local TINYINT(1) NOT NULL DEFAULT 0,
  national TINYINT(1) NOT NULL DEFAULT 0,
  international TINYINT(1) NOT NULL DEFAULT 0
);

Example server-side pattern (PDO, maps expected fields to 0/1, then inserts safely):

$fields = ['header','footer','local','national','international'];
$values = [];
foreach ($fields as $f) {
  $values[':'.$f] = !empty($_POST[$f]) ? 1 : 0;
}
$sql = 'INSERT INTO layout (' . implode(',', $fields) . ')
        VALUES (' . implode(',', array_keys($values)) . ')';
$stmt = $pdo->prepare($sql);
$stmt->execute($values);

Troubleshooting: var_dump($_POST) to see what is actually sent, confirm your input name attributes exactly match DB field names or the names in $fields, and enable PDO exceptions for errors. See the PHP PDO docs for safe DB access (PHP PDO manual), the HTML label element for clickable labels (MDN label), and MySQL integer types for column choice (MySQL integer types).

Recommended Answers

All 3 Replies

Here's a sample for a single checkbox, you can do the rest:

<form method="POST" action="">
<span class="label">Header</span>
<input type="checkbox" name="header" value="1">
<input type="submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])){
  $header = (isset($_POST['header'])) ? $_POST['header'] : "0" ;
  $insert = "INSERT INTO layout (header) VALUES ('$header')";
  mysql_query($insert); 
}
?>

What you simply need to know here is that with checkboxes, if it is not checked then it is NOT posted, which means it wont be set on the processing php file. If it is posted then the value you assigned it in the value attribute is what will present in $_POST.

So try it out for one, if it works for one it works for all ! :)

expanding on wilch's prior post, his(her?) answer is correct
the html <label> tag is terribly underused

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Create your news page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<fieldset>
<legend>Checked components will show in the page</legend>
<form method="POST" action="">
<label for='header' class='Header'>Header</label>
<input type="checkbox" name="header" id="header" value="1">
<br>
<label for='footer' class="label">Footer</label>
<input type="checkbox" id='footer' name="footer" value="1">

<hr>
<label for='local' class="label">Local news</label>
<input type="checkbox" id='local' name="local" value="1">

<input type="submit">
</form>
</fieldset>
</body>
</html>

<label>s make checkbox text clickable

hmm, interesting facts over there Almostbob, i had always wondered what makes some text on checkboxes clickable, on some not. Now, i know - thanks too !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.