nibble Posted September 4, 2009 Share Posted September 4, 2009 Hi... I'm new to PHP or any web coding and need some help.... I'm using.. Windows > WAMP > Apache | PHP | MySQL I'm trying to take a user input using check box and POST method. using the following code <form action="abc.php" method="post"> One: <input type="checkbox" name="checkbox" /> <input type="submit"/> When checked I get a value "on" echoed on using the >> echo $_POST["checkbox"]; << and when unchecked I get >> Undefined index: checkbox in... << message Any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/173143-checkbox-shows-as-undefined-when-unchecked/ Share on other sites More sharing options...
Philip Posted September 4, 2009 Share Posted September 4, 2009 if(isset($_POST['checkbox']) && !empty($_POST['checkbox'])) { // checkbox is checked } Quote Link to comment https://forums.phpfreaks.com/topic/173143-checkbox-shows-as-undefined-when-unchecked/#findComment-912598 Share on other sites More sharing options...
rhodesa Posted September 4, 2009 Share Posted September 4, 2009 yeah.....unchecked checkboxes don't get passed by the form Quote Link to comment https://forums.phpfreaks.com/topic/173143-checkbox-shows-as-undefined-when-unchecked/#findComment-912608 Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 if(isset($_POST['checkbox']) && !empty($_POST['checkbox'])) { // checkbox is checked } You can just do this: if(!empty($_POST['checkbox'])) { // checkbox is checked } It's impossible to have isset($_POST['checkbox']) == false and !empty($_POST['checkbox']) == true simultaneously. Quote Link to comment https://forums.phpfreaks.com/topic/173143-checkbox-shows-as-undefined-when-unchecked/#findComment-912616 Share on other sites More sharing options...
Philip Posted September 4, 2009 Share Posted September 4, 2009 But when you don't have the key checkbox won't it throw a notice for undefined index? Edit: answered my own question. I didn't know that empty wouldn't throw a notice empty() is the opposite of (boolean) var' date=' except that no warning is generated when the variable is not set.[/quote'] Quote Link to comment https://forums.phpfreaks.com/topic/173143-checkbox-shows-as-undefined-when-unchecked/#findComment-912619 Share on other sites More sharing options...
nibble Posted September 4, 2009 Author Share Posted September 4, 2009 Wow... Thanks a ton King.... that was pretty cool... any idea y it doesn't work like "text"? Edit: Guess the second question got answered too. Quote Link to comment https://forums.phpfreaks.com/topic/173143-checkbox-shows-as-undefined-when-unchecked/#findComment-912630 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.