doubledee Posted June 16, 2012 Share Posted June 16, 2012 On my "User Details" form, this are two radio buttons where a User can specify his/her "gender". (This is optional.) I am getting an Undefined Index error, which I think is happening because I do not have a default radio button set. (Since this is an optional field, I wouldn't want a default value.) I'm not really sure how to fix this so I don't get... Notice: Undefined index: gender in... on line 211 Here is my Form code... <!-- Gender --> <label>Gender:</label> <input type="radio" name="gender" value="1" <?php echo (isset($gender) && $gender == "1") ? 'checked="checked"' : ''; ?>/> Male <input type="radio" name="gender" value="2" <?php echo (isset($gender) && $gender == "2") ? 'checked="checked"' : ''; ?>/> Female <?php if (!empty($errors['gender'])){ echo '<span class="error">' . $errors['gender'] . '</span>'; } ?> And here is where my code is crashing... // ******************** // Compare Details. * // ******************** if (($s_firstName == $_POST['firstName']) && ($s_gender == $_POST['gender']) && ($s_birthYear == $_POST['birthYear']) && ($s_location == $_POST['location']) && ($s_occupation == $_POST['occupation']) && ($s_interests == $_POST['interests']) && ($s_aboutMe == $_POST['aboutMe'])){ How can I fix this? Is it possible to do this... isset($_POST['gender'] ? $_POST['gender'] : 0); BTW, ideally what I would like to do is have my Gender Radio Buttons default to "0", but not show anything on the Form. (Male=1, Female=2) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/ Share on other sites More sharing options...
xyph Posted June 16, 2012 Share Posted June 16, 2012 isset($_POST['gender'] ? $_POST['gender'] : 0); Should be $_POST['gender'] = isset($_POST['gender']) ? $_POST['gender'] : 0; A notice error won't 'crash' your code. To avoid confusion, please refrain from using incorrect terminology. Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354494 Share on other sites More sharing options...
Barand Posted June 16, 2012 Share Posted June 16, 2012 yes $gender = isset($_POST['gender'] ? $_POST['gender'] : 0); Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354495 Share on other sites More sharing options...
doubledee Posted June 16, 2012 Author Share Posted June 16, 2012 Should be $_POST['gender'] = isset($_POST['gender']) ? $_POST['gender'] : 0; Where should I put that code? In my PHP section or in my HTML Form section, or doesn't it matter? And to my other question... Is there a way to have my Gender Radio Buttons default to "0", but not show anything on the Form. (I mean we kind did that above, but if I forgot to write that code then I get errors, so I was hoping to have something built in to the Radio Buttons themselves, if you follow me?!) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354496 Share on other sites More sharing options...
Barand Posted June 16, 2012 Share Posted June 16, 2012 you could put a hidden input field in the form before the radio buttons with name = gender and value = 0 Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354497 Share on other sites More sharing options...
doubledee Posted June 16, 2012 Author Share Posted June 16, 2012 you could put a hidden input field in the form before the radio buttons with name = gender and value = 0 Any security risks in that? If you had to pick the first way or this way, which would you choose? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354498 Share on other sites More sharing options...
Barand Posted June 16, 2012 Share Posted June 16, 2012 Choose either. With the hidden field before the buttons the default will be "0". If, however, one of the buttons is clicked then that "gender" input will override the earlier hidden value with the same name. It does ensure that $_POST['gender'] always has a value. Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354499 Share on other sites More sharing options...
doubledee Posted June 16, 2012 Author Share Posted June 16, 2012 Choose either. With the hidden field before the buttons the default will be "0". If, however, one of the buttons is clicked then that "gender" input will override the earlier hidden value with the same name. It does ensure that $_POST['gender'] always has a value. Like this, right? <!-- Gender --> <label>Gender:</label> <input type="hidden" name="gender" value="0" checked="checked" /> <input type="radio" name="gender" value="1" <?php echo (isset($gender) && $gender == "1") ? 'checked="checked"' : ''; ?>/> Male <input type="radio" name="gender" value="2" <?php echo (isset($gender) && $gender == "2") ? 'checked="checked"' : ''; ?>/> Female <?php if (!empty($errors['gender'])){ echo '<span class="error">' . $errors['gender'] . '</span>'; } ?> Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354500 Share on other sites More sharing options...
xyph Posted June 16, 2012 Share Posted June 16, 2012 Where should I put that code? Somewhere before you attempt to access the variable. A line before the notice I'll help you with code, or logic, but doing both is getting close to coding for you. you could put a hidden input field in the form before the radio buttons with name = gender and value = 0 Good trick. Debbie, it's a good idea to check if EVERY post value exists before using it, not just gender. If, for some reason a blank field doesn't get sent, you might get unnecessary notices. I've modified an old snippet to fit your knowledge a little better, and expanded a little on the comments. It validates your $_POST data against an array you build, and validates/sanitizes if needed. It's a little overkill, but think of how often you build web forms. A re-usable function like this can save tons of time. <?php // set this in your config $update_form_fields = array( // name="foo" is an expected input, but not required. // set value to whatever default value you want if it's not set 'foo' => 'default', // name="bar" is a required input 'bar' => array(), // name="baz" is a required input, and it will be cast to integer 'baz' => array('validate'=>'int'), // name="cas", required, and it must match the RegEx provided 'cas' => array('validate'=>'regex','pattern'=>'/^[-_a-z0-9]+$/i') ); // include this with any other form functions you may have // function will return TRUE on success // array of errors on failure function form_validate( $fields, $sql_sanitize = FALSE ) { // $fields will be the above array we created // set $sql_sanitize to your MySQLi link when calling to call // whatever sql sanitize function you want. If using objects, // you could pass the actual object. // this array will store errors $errors = array(); // loop through fields foreach( $fields as $name => $field ) { // check if $_POST equivalent exists if( isset($_POST[$name]) ) { // check if field is required (an array) if( is_array($field) ) { // check if field was left empty if( trim($_POST[$name]) == '' ) { // Set an error message for the current field in the loop $errors[$name] = 'Required Field Left Empty'; // Continue skips the rest of the foreach loop, and starts // again on the next entry continue; // else if it's not empty, check if it needs to be validated } elseif( isset($field['validate']) ) { // Use a switch here, comparing one variable against a // bunch of values switch( $field['validate'] ) { case 'int': $_POST[$name] = (int)$_POST[$name]; break; case 'regex': if( !preg_match($field['pattern'], $_POST[$name]) ) $errors[$name] = 'Invalid Input'; break; } // end switch } // end validate check } // end required check // check if value should be sql-sanitized if( $sql_sanitize ) $_POST[$name] = mysqli_escape_string($sql_sanitize, $_POST[$name]); // Else, if the $_POST equivalent doesn't exist } else { // Check if it's required if( is_array($field) ) $errors[$name] = 'Required field not present'; // Otherwise, set it to default else $_POST[$name] = $field; }// end $_POST verification }// end loop // check if there are errors if( !empty($errors) ) return $errors; else return TRUE; }// end function // using the function and array above $db = mysqli_connect('localhost','root','','db'); // check for submission if( $_SERVER['REQUEST_METHOD'] == 'POST' ) { // debug echo 'POST array before:<pre>'; print_r($_POST); echo '</pre>'; // perform function, capture return $result = form_validate($update_form_fields, $db); // more debug echo 'POST array after:<pre>'; print_r($_POST); echo '</pre>'; // check if valid if( $result === TRUE ) echo '<h3>valid post</h3>'; else { echo '<h3>errors</h3><ul>'; foreach( $result as $name => $error ) echo '<li><b>'.$name.'</b> - '.$error.'</li>'; echo '</ul>'; } } ?> <form method="post" action=""> <p>Bar - required:<input type="text" name="bar"></p> <p>Baz - cast to int:<input type="text" name="baz"></p> <p>Cas - must match ^[-_a-z0-9]+$:<input type="text" name="cas"></p> <input type="submit"> </form> Also, you don't need checked="checked" for you hidden input Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354501 Share on other sites More sharing options...
doubledee Posted June 16, 2012 Author Share Posted June 16, 2012 you could put a hidden input field in the form before the radio buttons with name = gender and value = 0 Good trick. Debbie, it's a good idea to check if EVERY post value exists before using it, not just gender. If, for some reason a blank field doesn't get sent, you might get unnecessary notices. I agree, so I think it is better to use my (isset()) code instead... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264310-undefined-index-and-radio-buttons/#findComment-1354505 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.