Jacques Posted August 4, 2012 Share Posted August 4, 2012 I have added a "terms" check box to the user registration form in Chapter 18. The registration is successful even if the box is not ticked, although it prints the error message (when not ticked). Also, is there a way to make the "tick box" sticky like the rest of the registration form? I got the code I am using (below) from Larry's "PHP for the Web" book. // Validate the terms: if ( !isset($_POST['terms'])) { echo '<p class="error">You must accept the terms and conditions of use.</p>'; } Table field: <p><input type="checkbox" name="terms" value="Yes" /> I have read, understood and agree to the <a href="terms.php" title="View Terms and Conditions">terms and conditions</a> of use.</p> Link to comment Share on other sites More sharing options...
margaux Posted August 4, 2012 Share Posted August 4, 2012 To make a checkbox 'sticky' you can use something like <input type="checkbox" name="terms" value="Yes" <?php if (isset($_POST['terms']) && $_POST['terms'] == "Yes") {echo " checked";}?>/> As for your first question - we really need to see more of your code to see why the logic isn't working. Are you setting some kind of error flag or adding errors to an error array which is checked before the registration process is carried out? 1 Link to comment Share on other sites More sharing options...
Jacques Posted August 5, 2012 Author Share Posted August 5, 2012 Hi Margaux. I applied your code to my table and the tick box is now sticky, thank you! Regarding the validation code that I provided for the check box; that is unfortunately all I could get so far from Larry's PHP book. I just included it with the code of register.php in Chapter 18. I include the email validation code as an example: // Validate and match the email addresses: if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email1'])) { if ($trimmed['email1'] == $trimmed['email2']) { $e = mysqli_real_escape_string ($dbc, $trimmed['email1']); } else { echo '<p class="error">Your email address did not match the confirmed email address.</p>'; } } else { echo '<p class="error">Please enter your email address.</p>'; } // Validate the terms: if ( !isset($_POST['terms'])) { echo '<p class="error">You must accept the terms and conditions of use.</p>'; } // If the information passed: if ($fn && $ln && $in && $c && $e) { Link to comment Share on other sites More sharing options...
Edward Posted August 5, 2012 Share Posted August 5, 2012 I have added a "terms" check box to the user registration form in Chapter 18. The registration is successful even if the box is not ticked, although it prints the error message (when not ticked). Also, is there a way to make the "tick box" sticky like the rest of the registration form? I got the code I am using (below) from Larry's "PHP for the Web" book. // Validate the terms: if ( !isset($_POST['terms'])) { echo '<p class="error">You must accept the terms and conditions of use.</p>'; } Table field: <p><input type="checkbox" name="terms" value="Yes" /> I have read, understood and agree to the <a href="terms.php" title="View Terms and Conditions">terms and conditions</a> of use.</p> Why bother with the agreement check box, just put in a notice before the submit button of the form quoting "if you click the submit button below you are agreeing to our terms". I done this and its save the extra validation and time and looks better than that ugly checkbox. Link to comment Share on other sites More sharing options...
margaux Posted August 5, 2012 Share Posted August 5, 2012 The register script uses the following line of code to check that all validation checks have passed before continuing with the registration process. if ($fn && $ln && $e && $p) You could create another variable when you validate the checkbox and include it in this line e.g. if (isset($_POST['terms'])) { $terms=TRUE } else { echo '<p class="error">You must accept the terms and conditions of use.</p>'; } Then add $terms to the check that all validation has passed. if ($fn && $ln && $e && $p && $terms) and at the start of the script set $terms to false $fn = $ln = $e = $p = $terms = FALSE; 1 Link to comment Share on other sites More sharing options...
Edward Posted August 5, 2012 Share Posted August 5, 2012 The registration error array is easier it's saves you having to write that long if conditional then you can pass validation with a simple check to see whether you errors array is empty. Link to comment Share on other sites More sharing options...
Jacques Posted August 6, 2012 Author Share Posted August 6, 2012 Thank you for the code examples and suggestions Margaux and Edward. I applied the code and the registration form works as required. Great forum and book! Link to comment Share on other sites More sharing options...
Larry Posted August 9, 2012 Share Posted August 9, 2012 Thanks for the nice words and thanks to everyone for helping out! I really love to see that. Link to comment Share on other sites More sharing options...
Jacques Posted August 12, 2012 Author Share Posted August 12, 2012 I am using the below code to get 'countries' for my user registration form instead of hard coding them. The query works but I am however experiencing the following issues: 1. After submitting the completed form it gives the validation error message: "Please select your country." but the "country" list is blank, saved for the word "Country" at the top. <?php // Select a country: echo '<select name="country_id" method="get" id="country_id"> <option value="0">' . $words['country'] . '...</option>'; // Retrieve all the countries: $q = "SELECT country_id, country FROM countries ORDER BY country_eng ASC"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) > 0) { while ($menu_row = mysqli_fetch_array($r, MYSQLI_NUM)) { echo "<option value=\"$menu_row[0]\">$menu_row[1]</option>\n"; } } mysqli_free_result($r); echo '</select>'; ?> 2. How can I make the "Country" field sticky? Any advice will be much appreciated. Link to comment Share on other sites More sharing options...
Larry Posted August 13, 2012 Share Posted August 13, 2012 If the country list is blank, your query isn't returning anything. You'll need to check that. To make the SELECT sticky, you'll need to compare the previously-provided country value against the current country value within the while loop, and add selected="selected" when there's a match. Link to comment Share on other sites More sharing options...
Jacques Posted August 15, 2012 Author Share Posted August 15, 2012 Thank you for your help Larry. I found the complete required lines of code in Script 19.2, "Add a Print - Select an Artist"! Link to comment Share on other sites More sharing options...
Recommended Posts