Jacques Posted September 5, 2012 Share Posted September 5, 2012 I have added an "Accept Terms" check box to the registration form. It seems to work with the exception that it has no stickiness. Any help will be appreciated. Below my code snippets: form_functions.inc.php // Conditional to determine what kind of element to create: if ( ($type == 'text') || ($type == 'password') || ($type == 'checkbox') ) { // Create text or password or checkbox inputs. register.php - validation: // Validate the terms: if (isset($_POST['terms'])) { $t = TRUE; } else { // Terms not checked. $reg_errors['terms'] = 'You must accept the terms and conditions of use.'; } register.php - form <p><label for="terms"></label><?php create_form_input('terms', 'checkbox', $reg_errors); if (isset($_POST['terms']) && $_POST['terms'] == "Yes") echo "checked"; ?> <small>I have read and agree to the <a href="terms.php" title="View Terms and Conditions">terms and conditions</a> of use.</small></p> Thank you in advance. Link to comment Share on other sites More sharing options...
margaux Posted September 5, 2012 Share Posted September 5, 2012 Checkbox is a boolean so in your register.php file it should be && $_POST['terms'] == true Link to comment Share on other sites More sharing options...
Larry Posted September 6, 2012 Share Posted September 6, 2012 You have to put the logic for adding checked="checked" within the create_form_input() function (as with the other form elements). 1 Link to comment Share on other sites More sharing options...
Jacques Posted September 12, 2012 Author Share Posted September 12, 2012 Thank you for the response Larry and Margaux. I have managed to get the checkbox to work but just need confirmation from you on the "quality" of the code. form_functions script: } elseif ($type == 'checkbox') { // Create a checkbox. // Start creating the input: echo '<input type="' . $type . '" name="' . $name . '" id="' . $name . '"'; // Add the value to the input: if ($value == TRUE) echo 'checked="checked"'; // Check for an error: if (array_key_exists($name, $errors)) { echo 'class="error" /> <span class="error">' . $errors[$name] . '</span>'; } else { echo ' />'; } register script: // Validate the terms: if (isset($_POST['terms'])) { $t = TRUE; } else { $reg_errors['terms'] = 'You must accept the terms and conditions of use.'; } register script form: <p><label for="terms"></label><?php create_form_input('terms', 'checkbox', $reg_errors); ?> <small>I have read, understood and agree to the <a href="terms.php" title="View Terms and Conditions">terms and conditions</a> of use.</small></p> I also want to use data retrieved from the database for select menus in the form_functions script. The "require Config and MySQL" are contained in the header. I however get the following errors: Warning: print_r() [function.print-r]: Couldn't fetch mysqli_result in C:\xampp\htdocs\site\includes\config.inc.php on line 69 Warning: print_r() [function.print-r]: Property access is not allowed yet in C:\xampp\htdocs\site\includes\config.inc.php on line 69 The code is as follows: } elseif ($type == 'select') { // Create a select menu. if (($name == 'timezone')) { // Create a list of timezones. // Retrieve the timezones. $q = "SELECT timezone_id, timezone FROM timezones ORDER BY timezone ASC"; $r = mysqli_query ($dbc, $q); while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) { echo "<option value=\"$row[0]\""; } $data = $r; // Free the results: mysqli_free_result($r); Thank you in advance. Link to comment Share on other sites More sharing options...
Larry Posted September 14, 2012 Share Posted September 14, 2012 Well, that looks pretty good except that the value will depend upon the value of the checkbox. As for your other problem, since it's a different problem, it should go in a new thread. Link to comment Share on other sites More sharing options...
Jacques Posted September 14, 2012 Author Share Posted September 14, 2012 Hi Larry, Thank your for your continued advice. As I am still very new at web development I get lost very easy. What do you mean by "that the value will depend upon the value of the checkbox"? Do I have to change the value from "True" to "Yes"? I have started a new thread for my second question. Kind regards, Jacques Link to comment Share on other sites More sharing options...
Larry Posted September 14, 2012 Share Posted September 14, 2012 Well, what value does your checkbox have for its value attribute? Link to comment Share on other sites More sharing options...
Jacques Posted September 15, 2012 Author Share Posted September 15, 2012 I have used "YES" as the value attribute. Link to comment Share on other sites More sharing options...
Larry Posted September 17, 2012 Share Posted September 17, 2012 Okay, then the function should test against that. Link to comment Share on other sites More sharing options...
Jacques Posted September 23, 2012 Author Share Posted September 23, 2012 Thank you Larry. Link to comment Share on other sites More sharing options...
Recommended Posts