0

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="http://*********.php">
<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);
}
}
?>

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by wilch
0

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

<form method="POST" action="http://*********.php">
<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 ! :)

0

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="http://*********.php">
<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

0

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 !

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.