Tendency Posted June 13, 2012 Share Posted June 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264137-problems-with-defining-variables-and-checkbox-forms-please-help/ Share on other sites More sharing options...
Tendency Posted June 14, 2012 Author Share Posted June 14, 2012 Can anyone please help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/264137-problems-with-defining-variables-and-checkbox-forms-please-help/#findComment-1353652 Share on other sites More sharing options...
Drummin Posted June 14, 2012 Share Posted June 14, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/264137-problems-with-defining-variables-and-checkbox-forms-please-help/#findComment-1353670 Share on other sites More sharing options...
Tendency Posted June 15, 2012 Author Share Posted June 15, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/264137-problems-with-defining-variables-and-checkbox-forms-please-help/#findComment-1354041 Share on other sites More sharing options...
Drummin Posted June 15, 2012 Share Posted June 15, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264137-problems-with-defining-variables-and-checkbox-forms-please-help/#findComment-1354042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.