dan_t Posted May 2, 2010 Share Posted May 2, 2010 Choosing these from the checkbox list. Art<input type="checkbox" name="interest" value="art" /> Boating<input type="checkbox" name="interest" value="boating" /> Camping<input type="checkbox" name="interest" value="camping" /> Diving (Scuba)<input type="checkbox" name="interest" value="diveSc" /> Diving (Sky)<input type="checkbox" name="interest" value="diveSk" /> Equestrian <input type="checkbox" name="interest" value="esquest" /> Field sports<input type="checkbox" name="interest" value="field_sports" /> Fishing<input type="checkbox" name="interest" value="fish" /><br /> <br /> Golf<input type="checkbox" name="interest" value="golf" /> Hunting<input type="checkbox" name="interest" value="hunting" /> Jogging<input type="checkbox" name="interest" value="jog" /> Line dancing<input type="checkbox" name="interest" value="line_dance" /> Motor sports<input type="checkbox" name="interest" value="motor_sports" /> Music<input type="checkbox" name="interest" value="music" /> Racing<input type="checkbox" name="interest" value="racing" /> Skiing<input type="checkbox" name="interest" value="skiing" /> Tennis<input type="checkbox" name="interest" value="tennis" /> When you choose more than one and submit it, it only submits the last choice to the database, how do you enter more than one value? Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/ Share on other sites More sharing options...
Zane Posted May 2, 2010 Share Posted May 2, 2010 when you name them all the same ... "interest" you have to put braces beside it or else it just overwrites "interests[]" Note: this will give you an array in your POST or GET request. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052103 Share on other sites More sharing options...
dan_t Posted May 2, 2010 Author Share Posted May 2, 2010 So you put braces beside them in the form? Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052104 Share on other sites More sharing options...
Zane Posted May 2, 2010 Share Posted May 2, 2010 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052108 Share on other sites More sharing options...
dan_t Posted May 2, 2010 Author Share Posted May 2, 2010 OK, now it enters the word "array" into the field "interest" , but not any values. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052119 Share on other sites More sharing options...
Zane Posted May 2, 2010 Share Posted May 2, 2010 when you name them all the same ... "interest" you have to put braces beside it or else it just overwrites "interests[]" Note: this will give you an array in your POST or GET request. $_POST['interest'][0] should have something in it. If a person checked something. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052121 Share on other sites More sharing options...
dan_t Posted May 2, 2010 Author Share Posted May 2, 2010 I get this: Fatal error: Cannot use [] for reading in C:\wamp\www\bills\show_info.php on line 19 trying to use this: $interest = $_POST['interest'][]; Just thought I would try-no such luck. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052127 Share on other sites More sharing options...
dan_t Posted May 2, 2010 Author Share Posted May 2, 2010 Why would it report back just array, instead of some type of value? Should I put a value in each one and then use an if statement? like: interest[0] and then: if(interest[0]) { $interest = $_POST['interest']; } Or would that just overcomplicate for no reason? Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052137 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 I think you need to read up on arrays. $_POST['interest'] is an array of values. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052148 Share on other sites More sharing options...
dan_t Posted May 3, 2010 Author Share Posted May 3, 2010 I think you need to read up on arrays. $_POST['interest'] is an array of values. I got it without the arrays, but I appreciate your wisdom. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052193 Share on other sites More sharing options...
dan_t Posted May 3, 2010 Author Share Posted May 3, 2010 I've got everything working, but i get these: Notice: Undefined index: interest in C:\wamp\www\bills\show_info.php on line 33 Notice: Undefined index: interest2 in C:\wamp\www\bills\show_info.php on line 35 Notice: Undefined index: interest3 in C:\wamp\www\bills\show_info.php on line 36 Notice: Undefined index: interest4 in C:\wamp\www\bills\show_info.php on line 37 Notice: Undefined index: interest5 in C:\wamp\www\bills\show_info.php on line 38 Notice: Undefined index: interest6 in C:\wamp\www\bills\show_info.php on line 39 Notice: Undefined index: interest7 in C:\wamp\www\bills\show_info.php on line 40 Notice: Undefined index: interest8 in C:\wamp\www\bills\show_info.php on line 41 Notice: Undefined index: interest9 in C:\wamp\www\bills\show_info.php on line 42 Notice: Undefined index: interest11 in C:\wamp\www\bills\show_info.php on line 44 Notice: Undefined index: interest12 in C:\wamp\www\bills\show_info.php on line 45 Notice: Undefined index: interest13 in C:\wamp\www\bills\show_info.php on line 46 Notice: Undefined index: interest14 in C:\wamp\www\bills\show_info.php on line 47 Notice: Undefined index: interest15 in C:\wamp\www\bills\show_info.php on line 48 with the unused checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052194 Share on other sites More sharing options...
dan_t Posted May 3, 2010 Author Share Posted May 3, 2010 Zanus, you have been a lot of help. Have you got an answer for this one? I hope it's an easy fix. Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052195 Share on other sites More sharing options...
dan_t Posted May 3, 2010 Author Share Posted May 3, 2010 The missing numbers were to ones that were checked. I read some where you can fix a problem like this by doing something like: $interest = $interest$_POST['interest'] = " "; or something like that. Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052196 Share on other sites More sharing options...
Zane Posted May 3, 2010 Share Posted May 3, 2010 you had it right the first time... using the array solution. Having 15+ differently named checkboxes is by no means a more efficient way of doing this. The reason you get the undefined index is because all of those checkboxes aren't checked.. and you're coding it in such a way that all of them would be checked. By using "interest[]" as the name for all of them.. you can then access them like this $_POST['interest'][0] and $_POST['interest'][1] for the next (checked) one... and so on and so on. It's also good practice (in PHP) to use the isset() function. ... example if(isset($_POST['interest'])) { echo $_POST['interest'][0]; } or ... you can check if it's actually an array too. if(isset($_POST['interest']) AND is_array($_POST['interest'])) { echo $_POST['interest'][0]; } Quote Link to comment https://forums.phpfreaks.com/topic/200492-submitting-with-checkboxes/#findComment-1052213 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.