Jump to content
Larry Ullman's Book Forums

Recommended Posts

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

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?

  • Upvote 1
Link to comment
Share on other sites

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

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

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;

  • Upvote 1
Link to comment
Share on other sites

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

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

 Share

×
×
  • Create New...