Jump to content

Problems With Defining Variables and Checkbox forms. Please help.


Tendency

Recommended Posts

Hello everyone, I'm a biophysics grad trying to learn php. To my handicap, not many tutorials on the net are written in a way of learning that makes good sense (induction). Besides the point, I am having issues such as this:

 

Notice: Undefined variable: color in /var/www/cars/box.php on line 12

Notice: Undefined variable: dbcolors in /var/www/cars/box.php on line 12

 

Html file:

<!DOCTYPE html>
<html>
<head>
<title>Color Selection</title>
</head>

<body>

<form name="form1" method="post" action="">
<div>
<p>
<input name="colors[]" type="checkbox" id="colors[]" value="Red">
Red
<input name="colors[]" type="checkbox" id="colors[]" value="Blue">
Blue
<input name="colors[]" type="checkbox" id="colors[]" value="Green">
Green</p>
<p>
<input name="colors[]" type="checkbox" id="colors[]" value="Yellow">
Yellow
<input name="colors[]" type="checkbox" id="colors[]" value="Brown">
Brown </p>
</form>
<input style="color:purple;" type="submit" name="submit" value="Select My Car">
</div>
</form>
</body>
</html>

<?php


include('database.php');

if (isset($_POST['submit']))
{
$colors = mysql_real_escape_string(implode(',', $_POST['colors']));

mysql_query("INSERT colors SET colors='$colors'")
  or die(mysql_error());

header("Location: box.php");
  }

 

Edit file:

require_once('database.php');
$aColors = array("Red", "Blue", "Green", "Yellow", "Brown");

$dbcolors= explode(',',$row['color']);

foreach ($aColors as $color) {

if(in_array($color,$dbcolors)) {
  echo "<input name=\"colors[]\" type=\"checkbox\" value=\"$color\" CHECKED> $facil ";
      } else
      {
  echo "<input name=\"colors[]\" type=\"checkbox\" value=\"$color\"> $color";
              }
          }

 

Thanks All.

Link to comment
Share on other sites

Here's a single page example.  You listed the table field as "colors" in processing and "color" for your $row field.  This example uses "colors" as the field name.  I commented out the header in this single page example as it's not needed.

<?php
include('database.php');

if (isset($_POST['submit']) && isset($_POST['colors']))
{
	$colors = mysql_real_escape_string(implode(',', $_POST['colors']));
	mysql_query("INSERT INTO colors (colors) VALUES('$colors')")
	or die(mysql_error());
	//header("Location: box.php");
	//exit;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Color Selection</title>
</head>
<body>

<form method="post" action="">
<div>
<?php
$aColors = array("Red", "Blue", "Green", "Yellow", "Brown");
$sql = "SELECT colors FROM colors";
$result=mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);

$dbcolors= explode(',',$row['colors']);

foreach ($aColors as $color) {
	$checked = (in_array($color,$dbcolors) ? 'checked="checked"' : '');
	echo "<input name=\"colors[]\" type=\"checkbox\" value=\"$color\" $checked /> $color ";
}
?>
<input style="color:purple;" type="submit" name="submit" value="Select My Car" />
</div>
</form>
</body>
</html>

Link to comment
Share on other sites

Drummin, thank you for the reply. Though I was screaming in space.

 

I am trying to figure out the exact concept of how to repopulate those checkboxes bases upon choices in the database. Could you explain what happens when there are two pages and the form is submitted with checkbox values on it, enroute to the mysql and then what should happen in the edit page?

Link to comment
Share on other sites

The principle is the same only you are processing on a different page then using the header to send person back to original page.

Let say processing is on processing.php. it would be.

 

<?php
include('database.php');

if (isset($_POST['submit']) && isset($_POST['colors']))
{
	$colors = mysql_real_escape_string(implode(',', $_POST['colors']));
	mysql_query("INSERT INTO colors (colors) VALUES('$colors')")
	or die(mysql_error());
	header("Location: box.php");
	exit;
}

?>

 

As you had the header point to box.php I'll put the form on that page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Color Selection</title>
</head>
<body>

<form method="post" action="processing.php">
<div>
<?php
include('database.php');
$aColors = array("Red", "Blue", "Green", "Yellow", "Brown");
$sql = "SELECT colors FROM colors";
$result=mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);

$dbcolors= explode(',',$row['colors']);

foreach ($aColors as $color) {
	$checked = (in_array($color,$dbcolors) ? 'checked="checked"' : '');
	echo "<input name=\"colors[]\" type=\"checkbox\" value=\"$color\" $checked /> $color ";
}
?>
<input style="color:purple;" type="submit" name="submit" value="Select My Car" />
</div>
</form>
</body>
</html>

 

This is still a vary basic example and I'm sure you'll be wanting to identify the table row and do updates based on other factors.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.