Mark82 Posted July 5, 2008 Share Posted July 5, 2008 Hi I have a form with a series of 8 checkboxes. The user can select as many checkboxes as they wish (i.e. none or all . The values will be placed in a mysql databse. My problem is, if the user doesnt check a checkbox, the value isnt sent via POST and the error.... ERROR: UNDEFINED INDEX appears. Is there a method to have a default value, ro check whether it hasn't been checked etc? Any help appreicated, thanks. Mark Link to comment https://forums.phpfreaks.com/topic/113326-checkboxes-not-set-value/ Share on other sites More sharing options...
papaface Posted July 5, 2008 Share Posted July 5, 2008 Show us your current code. Link to comment https://forums.phpfreaks.com/topic/113326-checkboxes-not-set-value/#findComment-582228 Share on other sites More sharing options...
Mark82 Posted July 5, 2008 Author Share Posted July 5, 2008 This example only has 3 checkboxes, but the principle is still the same: <?php require_once("dbFunctions.php"); dbConnect(); if(isset($_POST['Submit'])) { $name = $_POST['txtname']; $opt1 = $_POST['checkbox']; $opt2 = $_POST['checkbox2']; $opt3 = $_POST['checkbox3']; $query="INSERT INTO alloyd (name, option1, option2, option3) VALUES ('$name', '$opt1', '$opt2', '$opt3');"; $result = dbQuery($query); echo "Results Added!"; } ?> Link to comment https://forums.phpfreaks.com/topic/113326-checkboxes-not-set-value/#findComment-582229 Share on other sites More sharing options...
papaface Posted July 5, 2008 Share Posted July 5, 2008 <?php require_once("dbFunctions.php"); dbConnect(); if(isset($_POST['Submit'])) { if (isset($_POST['txtname'])) $name = $_POST['txtname'] else $name = ""; if (isset($_POST['checkbox'])) $opt1 = $_POST['checkbox']; else $opt1 = ""; if (isset($_POST['checkbox2'])) $opt2 = $_POST['checkbox2']; else $opt2 = ""; if (isset($_POST['checkbox3'])) $opt3 = $_POST['checkbox3']; else $opt3 = ""; $query="INSERT INTO alloyd (name, option1, option2, option3) VALUES ('$name', '$opt1', '$opt2', '$opt3');"; $result = dbQuery($query); echo "Results Added!"; } ?> Very bad way to do it, but it utilizes your current code. Technically, you might aswell just do: <?php require_once("dbFunctions.php"); dbConnect(); if(isset($_POST['Submit'])) { $query="INSERT INTO alloyd (name, option1, option2, option3) VALUES ('".$_POST['txtname']."', '".$_POST['checkbox']."', '".$_POST['checkbox2']."', '".$_POST['checkbox3']."');"; $result = dbQuery($query); echo "Results Added!"; } ?> Link to comment https://forums.phpfreaks.com/topic/113326-checkboxes-not-set-value/#findComment-582231 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.