Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
Marie

Form Validation - Radio Buttons

Recommended Posts

Hello,

 

I have been trying to get my form to validate the radio button which allows my user to agree to the terms and conditions, however, it either prints at the top of the page or not at all. The other prompts print near the form input which is what I would like to happen for the radio button.

 

Following is my code.

 

if (isset($_POST['radio_Agree'])) {

$Agree = mysqli_real_escape_string($connect, $_POST['Agree']);

} else {

$reg_errors['radio'] = 'Please agree to the user Terms and Conditions.';

}

 

 

 

<p><label> <input type="radio" name="Agree" value="Agree" /></label>

<a name="Agree" id="Agree"></a>I agree to the user Terms and Conditions.<br />

<?php create_form_input('radio', 'Agree', $reg_errors); ?></p>

Share this post


Link to post
Share on other sites

Thanks,

 

Okay I just changed that and when I submit the form, all the other warnings are being printed but the – 'Please agree to the user Terms and Conditions.', which has been my main problem.

Share this post


Link to post
Share on other sites

Sorry, I didn't read your original post properly. You need to check that the radio button is checked.

if (isset($_POST['Agree']) && $_POST['Agree'] == 'checked') ) {

  • Upvote 1

Share this post


Link to post
Share on other sites

Marie, I'm not sure what your create_form_input function does (and I'm confused about some of your HTML), but the following is a very simple works-out-of-the-box demonstration of how to handle radio button logic.

 

<!DOCTYPE html>

<html lang="en">

 <head>

   <meta charset="UTF-8">

   <title>Radio button test</title>

 </head>

 <body>

   <?php

     if (isset($_POST['consent'])) {

       echo 'A radio button was selected.<br>';

       if ($_POST['consent'] === 'agree') {

         echo 'And the user agreed.';

       } else if ($_POST['consent'] === 'dont_agree') {

         echo 'But the user didn\'t agreed.';

       }

     } else {

       echo 'No radio buttons were selected.';

     }

   ?>

   <form action="" method="post">

     <input type="radio" name="consent" value="agree"> I agree

     <input type="radio" name="consent" value="dont_agree"> I don't agree

     <input type="submit" value="Submit">

   </form>

 </body>

</html>

 

The first thing you should do is test whether any radio buttons are selected or not with the isset function.

After that, you should then check to see which radio button was clicked. The value attributes of the input elements in the HTML form are the possible values that can be stored in the $_POST['consent'] array element when a radio button is selected.

 

If you want to output an error to right below the radio buttons, then just move the PHP logic/output to right after the radio buttons. Without seeing your create_form_input function, it's hard to comment beyond that, but I imagine that the create_form_input function is indeed the problem with your code.

 

As a side, you shouldn't give your a element the same name and ID as the input element. That could cause some issues in some browsers, especially if you are using Javascript. Just a little tip.

Also, what are you using an empty a element for (which doesn't have an href attribute, by the way)?

  • Upvote 1

Share this post


Link to post
Share on other sites

Below is what I have for the validation code and then the form. I didn't want to post too much code so I guess I didn't post enough. Basically I was trying to follow along with the code that I already have for the other form elements. So I haven't tried the futher suggestions yet, in case the solution would be very simple.

 

The page was working fine before I added the radio button and the "Agree" was being added to the database. However, IF the person didn't check the radio button, there was no warning being echoed.

 

 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 

if (isset($_POST['Agree'])) {

$Agree = mysqli_real_escape_string($connect, $_POST['Agree']);

} else {

//print $reg_errors['radio_Agree'] = 'Please agree to the user Terms and Conditions.';

$reg_errors[] = 'Please agree to the user Terms and Conditions.';

}

 

// Check for a first name:

if (preg_match ('/^[A-Z \'.-]{2,20}$/i', stripslashes($_POST['first_name']))) {

$fn = mysqli_real_escape_string ($connect, $_POST['first_name']);

} else {

$reg_errors['first_name'] = 'Please enter your first name or NA!';

}

 

// Check for a middle name:

if (preg_match ('/^[A-Z \'.-]{1,20}$/i', stripslashes($_POST['mid_initial']))) {

$mi = mysqli_real_escape_string ($connect, $_POST['mid_initial']);

} else {

$reg_errors['mid_initial'] = 'Please enter your middle name or initial or NA!';

}

 

// Check for a last name:

if (preg_match ('/^[A-Z \'.-]{2,40}$/i', stripslashes($_POST['last_name']))) {

$ln = mysqli_real_escape_string ($connect, $_POST['last_name']);

} else {

$reg_errors['last_name'] = 'Please enter your last name or NA!';

}

 

// Check for a username:

if (preg_match ('/^[A-Z0-9 \'.-]{2,30}$/i', stripslashes($_POST['username']))) {

$u = mysqli_real_escape_string ($connect, $_POST['username']);

} else {

$reg_errors['username'] = 'Please enter a desired name!';

}

 

// Check for an email address:

if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

$e = mysqli_real_escape_string ($connect, $_POST['email']);

} else {

$reg_errors['email'] = 'Please enter a valid email address!';

}

 

// Check for a password and match against the confirmed password:

if (preg_match ('/^(\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*){6,20}$/', $_POST['pass1']) ) {

if ($_POST['pass1'] == $_POST['pass2']) {

$p = mysqli_real_escape_string ($connect, $_POST['pass1']);

} else {

$reg_errors['pass2'] = 'Your password did not match the confirmed password!';

}

} else {

$reg_errors['pass1'] = 'Please enter a valid password!';

}

 

if (empty($reg_errors)) { // If everything's OK...

 

 

<form action="Register.php" method="post" accept-charset="utf-8" style="padding-left:200px">

 

<p><strong> All registered users must be of 18 years of age or older and agree<br /> to the Terms and Conditions in the following link <a href="UserAgree.html">Terms and Conditions</a>.</strong></p>

 

<p><label> <input type="radio" name="Agree" value="Agree" /></label>

<a name="Agree" id="Agree"></a>I agree to the user Terms and Conditions.<br />

<?php create_form_input('radio', 'Agree', $reg_errors); ?></p>

 

<p><label for="first_name"><strong>First Name</strong></label><br />

<p class="noticeType">Please enter your first name or NA if non applicable.</p>

<?php create_form_input('first_name', 'text', $reg_errors); ?></p>

 

<p><label for="mid_initial"><strong>Middle Name or Initial</strong></label><br />

<p class="noticeType">Please enter your middle name or initial or NA if non applicable.</p>

<?php create_form_input('mid_initial', 'text', $reg_errors); ?></p>

 

<p><label for="last_name"><strong>Last Name</strong></label><br />

<p class="noticeType">Please enter your last name or initial or NA if non applicable.</p>

<?php create_form_input('last_name', 'text', $reg_errors); ?></p>

 

<p><label for="username"><strong>Desired Username</strong></label><br />

<p class="noticeType">Only a combination of letters and numbers are <br />allowed with no special characters — from<br />

two to thirty in total.</p>

<?php create_form_input('username', 'text', $reg_errors); ?> </p>

 

<p><label for="email"><strong>Email Address</strong></label><br />

<p class="noticeType"> Please enter a valid email address and check your spelling.<br />Our website will recognize a proper email format but not typrographical errors.</p><?php create_form_input('email', 'text', $reg_errors); ?></p>

 

<p><label for="pass1"><strong>Password</strong></label><br />

<p class="noticeType"> Must be between 6 and 20 characters long, with at least one lowercase letter, <br />

one uppercase letter, and one number.</p>

<?php create_form_input('pass1', 'password', $reg_errors); ?></p>

 

<p><label for="pass2"><strong>Confirm Password</strong></label><br /></p>

<?php create_form_input('pass2', 'password', $reg_errors); ?></p>

 

<p><input type="submit" name="submit_button" value="Register →" id="submit_button" class="formbutton" /></p>

 

</form>

Share this post


Link to post
Share on other sites

I think you actually want to use a checkbox for your agree to T&C and I agree with HartleySan so I've removed the empty a element as it isn't doing anything.

 

Change your form to


<p><?php create_form_input('Agree', 'checkbox', $reg_errors); ?><label for="Agree"> I agree to the user Terms and Conditions.</label></p>

and you will need to ensure that your create_form_input function includes some code for input type checkbox. I don't have this book but something like

if (($type == 'text') || ($type == 'password') || ($type == 'checkbox')) {

Your validation will then be

if (isset($_POST['Agree']) && ($_POST['Agree'] == TRUE)) {
$Agree = mysqli_real_escape_string($connect, $_POST['Agree']);
} else {
 $reg_errors['Agree'] = 'Please agree to the user Terms and Conditions.';
 }

 

You have some non-standard practices in your code such as including styling within the html, using a capital letter in your your variables e.g. 'Agree' and not using ascii codes for special chars in your html e.g. change '-' to . I hope you don't mind my pointing these out but its good idea to use good practices. On some forums (not this one) people won't help unless you use standard practices.

Share this post


Link to post
Share on other sites

Thank you, I do appreciate all comments. I didn't realize that I was using some non-standard practices.

I had used a capital letter on the "Agree" column in the database so this is why I was using a capital in my code, but I have changed this in all areas.

 

Using a checkbox is a better idea.

 

I have reworked some of the form code many times since seeing your above reply and have posted it the way it is currently. It seems I am having the same problem as I did when a radio button was included. I am not getting the error message AND it seems the rest of the information will still be sent to the database whether the checkbox is checked or not. Obviously I only want anything to go to the database when everything is filled out.

 

I would rather not use Javascript and don't understand why php would work with the rest of the fields but not with a checkbox or radio button.

 

Thanks for your help

 

 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 

// Check for an agreement:

if (isset($_POST['agree']) && ($_POST['agree'] == TRUE)) {

$agree = mysqli_real_escape_string($connect, $_POST['agree']);

} else {

$reg_errors['agree'] = 'Please agree to the user Terms and Conditions.';

}

 

// Check for a first name:

if (preg_match ('/^[A-Z \'.-]{2,20}$/i', stripslashes($_POST['first_name']))) {

$fn = mysqli_real_escape_string ($connect, $_POST['first_name']);

} else {

$reg_errors['first_name'] = 'Please enter your first name or NA!';

}

 

// Check for a middle name:

if (preg_match ('/^[A-Z \'.-]{1,20}$/i', stripslashes($_POST['mid_initial']))) {

$mi = mysqli_real_escape_string ($connect, $_POST['mid_initial']);

} else {

$reg_errors['mid_initial'] = 'Please enter your middle name or initial or NA!';

}

 

// Check for a last name:

if (preg_match ('/^[A-Z \'.-]{2,40}$/i', stripslashes($_POST['last_name']))) {

$ln = mysqli_real_escape_string ($connect, $_POST['last_name']);

} else {

$reg_errors['last_name'] = 'Please enter your last name or NA!';

}

 

// Check for a username:

if (preg_match ('/^[A-Z0-9 \'.-]{2,30}$/i', stripslashes($_POST['username']))) {

$u = mysqli_real_escape_string ($connect, $_POST['username']);

} else {

$reg_errors['username'] = 'Please enter a desired name!';

}

 

// Check for an email address:

if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

$e = mysqli_real_escape_string ($connect, $_POST['email']);

} else {

$reg_errors['email'] = 'Please enter a valid email address!';

}

 

// Check for a password and match against the confirmed password:

if (preg_match ('/^(\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*){6,20}$/', $_POST['pass1']) ) {

if ($_POST['pass1'] == $_POST['pass2']) {

$p = mysqli_real_escape_string ($connect, $_POST['pass1']);

} else {

$reg_errors['pass2'] = 'Your password did not match the confirmed password!';

}

} else {

$reg_errors['pass1'] = 'Please enter a valid password!';

}

 

if (empty($reg_errors)) { // If everything's OK...

 

 

 

<form action="Register.php" method="post" accept-charset="utf-8" style="padding-left:200px">

 

<p><strong> All registered users must be of 18 years of age or older and agree<br /> to the Terms and Conditions in the following link <a href="UserAgree.html">Terms and Conditions</a>.</strong></p>

 

 

<p class="noticeType"><input type="checkbox" name="agree" value="agree" />I agree to the user Terms and Conditions.</p>

<?php create_form_input('agree', 'checkbox', $reg_errors); ?>

 

<p><label for="first_name"><strong>First Name</strong></label><br />

<p class="noticeType">Please enter your first name or NA if non applicable.</p>

<?php create_form_input('first_name', 'text', $reg_errors); ?></p>

 

<p><label for="mid_initial"><strong>Middle Name or Initial</strong></label><br />

<p class="noticeType">Please enter your middle name or initial or NA if non applicable.</p>

<?php create_form_input('mid_initial', 'text', $reg_errors); ?></p>

 

<p><label for="last_name"><strong>Last Name</strong></label><br />

<p class="noticeType">Please enter your last name or initial or NA if non applicable.</p>

<?php create_form_input('last_name', 'text', $reg_errors); ?></p>

 

<p><label for="username"><strong>Desired Username</strong></label><br />

<p class="noticeType">Only a combination of letters and numbers are <br />allowed with no special characters — from<br />

two to thirty in total.</p>

<?php create_form_input('username', 'text', $reg_errors); ?> </p>

 

<p><label for="email"><strong>Email Address</strong></label><br />

<p class="noticeType"> Please enter a valid email address and check your spelling.<br />Our website will recognize a proper email format but not typrographical errors.</p><?php create_form_input('email', 'text', $reg_errors); ?></p>

 

<p><label for="pass1"><strong>Password</strong></label><br />

<p class="noticeType"> Must be between 6 and 20 characters long, with at least one lowercase letter, <br />

one uppercase letter, and one number.</p>

<?php create_form_input('pass1', 'password', $reg_errors); ?></p>

 

<p><label for="pass2"><strong>Confirm Password</strong></label><br /></p>

<?php create_form_input('pass2', 'password', $reg_errors); ?></p>

 

<p><input type="submit" name="submit_button" value="Register →" id="submit_button" class="formbutton" /></p>

 

</form>

Share this post


Link to post
Share on other sites

Marie, I'm on my phone now, so I can't take a close look at your code, but I can say this:

Radio buttons and checkboxes are special in that if you don't select anything, nothing gets posted to PHP. This is different from the other types of input, which will always post at least something, no matter what.

 

As such, with radio buttons and checkboxes, you have to first check for the existence of a value before you can even start parsing the value.

Without looking at your code, I have a feeling that this is the problem.

 

Also, please put your code samples within code tags from now on. It makes it a lot easier to look at. Thank you.

Share this post


Link to post
Share on other sites

The code I previously posted worked. I think your problem is that you have 2 inputs for the checkbox.

<p class="noticeType"><input type="checkbox" name="agree" value="agree" />I agree to the user Terms and Conditions.</p>
<?php create_form_input('agree', 'checkbox', $reg_errors); ?>

replace the code above with

<p class="noticeType"><?php create_form_input('agree', 'checkbox', $reg_errors); ?><label for="agree">I agree to the user Terms and Conditions.</label>
</p>

Share this post


Link to post
Share on other sites

Marie, right after the if ($_SERVER['REQUEST_METHOD'] == 'POST') { line, add the following code:

 

echo '<pre>';

print_r($_POST);

echo '<pre>';

 

Use that code to analyze your entire $_POST array. You will very easily be able to see exactly what values are (or aren't) being sent to the $_POST array.

With that code in place, try both selecting and not selecting the checkbox, and see how it affects your $_POST array.

 

Similarly, once you have resolved the problem with the $_POST array and you are getting the expected value, next add the following code right before the if (empty($reg_errors)) { // If everything's OK... line:

 

 

echo '<pre>';

print_r($reg_errors);

echo '<pre>';

 

Similar to the code before, this will allow you to easily analyze your errors array.

Once you have a clear view of the exact state of the $_POST array and the errors array, you should be able to figure out where the problem in the logic is.

 

Please do the above and report back any issues you have. Also, PLEASE put your code without code tags. Thank you.

As a last piece of advice, make sure that your $connect variable for the mysqli_real_escape_string function is valid.

  • Upvote 1

Share this post


Link to post
Share on other sites

I have done some reworking and believe my problems were in the form functions page as you have suggested. However, some new problems have developed and I would say that these are also in the same form functions page.

 

I am now getting an error message but it is showing up between the check box and the form wording.

 

Also, the word "on" is being entered into the database rather than the word "Agree".

 

I would just like to clarify a few things from your previous posts.

 

"not using ascii codes for special chars in your html e.g. change '-' to ."

Are you referring to this type of code? '/^[A-Z \'.-]{2,20}$/i', so I should be entering '/^[AZ \'.]{2,20}$/i',

 

"including styling within the html"

Are up referring to the following type of code? <strong>First Name</strong> Should I be using css here?

 

"Also, please put your code samples within code tags from now on. It makes it a lot easier to look at. Thank you." and "Also, PLEASE put your code without code tags." I am not sure if you are talking about the same thing.

 

Following is my code from the forms function page within the code tags that are on this page. Is this the proper format?

 

} elseif ($type == 'checkbox') { // Create a CHECKBOX.
 // Start creating the input:
 echo '<input type="' . $type . '" name="' . $name . '" id="' . $name . '" true"';

// Add the value to the input:
 if ($value) echo ' value="' . htmlspecialchars($value) . '"';

// Display the error:
 if (array_key_exists($name, $errors)) {
  echo 'class="error" /> <span class="error">' . $errors[$name] . '</span>';
 } else {
  echo ' />'; 
 }

 

It is great that you people are willing to help, so I would like to follow the prescribed methods.

 

Thanks,

Marie

Share this post


Link to post
Share on other sites

Marie, it's hard to speculate why "on" is being put into your DB without seeing more code. It's the same advice as before, but I highly recommend echoing lots and lots of values out to the screen each step of the way. Hopefully that'll help you catch the error. If the error doesn't seem to be in the PHP, then it's always possible that the way you have structured your DB is causing the problem. Unfortunately, I think this is something you're going to have to debug on your own, as we're not going to take the time to look through all your code.

 

margaux was the one that mentioned using HTML entities (e.g., ) in your HTML. Honestly, I don't think it's that necessary, but all the same, to answer your question, his suggestion does not apply to things like '/^[A-Z \'.-]{2,20}$/i'. That is a regular expression and it is part of your PHP code. If you do decide to use HTML entities, then you'd only apply them to your HTML.

 

The statement about not using inline styles was also made by margaux. He was referring to things like the following:

 

<form action="Register.php" method="post" accept-charset="utf-8" style="padding-left:200px">

 

The style attribute should be removed from the form opening tag and CSS should be used instead. Things like using strong tags are completely acceptable and expected. That's not a problem.

 

Both of the statements about formatting your code were from me, and yes, both were asking you to do the same thing. I said it twice because even after I said it once, you still posted a lot of code without formatting it with code tags, and it was really hard to read. Luckily, this time, you did use code tags and your code was very easy to read. Thank you for doing that.

 

As for your code snippet, almost everything looks okay. I can't test the code out, but I'm a little confused by the following part:

 

<span class="error">' . $errors[$name] . '</span>'

 

Is that giving you what you want when an error occurs?

I'll leave it at that for now.

Share this post


Link to post
Share on other sites

Without seeing your d/b insert statement I can only guess why it's storing 'on'. Is your insert query something like

$q = "INSERT INTO tablename (first_name, mid_initial, last_name, username, email, pass, agree) VALUES ('$fn' ,'$mi', '$ln', '$u', '$e', '$p', '$agree' )";

 

If it is, then you need to change the following line of code

if (isset($_POST['agree']) && ($_POST['agree'] == TRUE)) {
$agree = mysqli_real_escape_string($connect, $_POST['agree']);
} else {

If the checkbox is checked, $_POST['agree']'s value will be 'on' which is what you are then storing in $agree. You could change the above code to

if (isset($_POST['agree']) && ($_POST['agree'] == TRUE)) {
$agree = 'Agree';
} else {

 

the comment about 'ascii codes' refers to this line

<p><label for="username"><strong>Desired Username</strong></label><br />
<p class="noticeType">Only a combination of letters and numbers are <br />allowed with no special characters — from<br />
two to thirty in total.</p>
<?php create_form_input('username', 'text', $reg_errors); ?> </p>

Some browsers will render the dash differently. Replacing the dash with its ascii equivalent, in this case , will ensure it renders correctly in all browsers. Not a big thing but if you have a funny character on your site, it makes your site look unprofessional.

 

Inline styling - HartleySan covered that. Best practice is to put all your css rules in an external file sheet and in your head section provide a link to it.

 

<span class="error">' . $errors[$name] . '</span>'

The above code displays your error messages and you may want to add additional css rules to control how your error messages are displayed.

 

I completely agree with HartleySan's suggestion re liberally sprinkling print_r statements throughout your code during development. Not only will it help you with debugging, you will also learn alot about the internal workings of php by looking at what and how it stores your variables and global variables.

Share this post


Link to post
Share on other sites

Just to add to what margaux said, he is right in that using a regular dash could potentially cause some rendering (i.e., encoding) issues in some browsers. As far as I know though, if you use UTF-8 encoding for your pages (which I always do), then this should never be a problem and you should never have to worry about using HTML entities for things like dashes.

Share this post


Link to post
Share on other sites

Thank you both for you help. Everything that was suggested was absolutely correct. My page is working well enough right now and the word "Agree" is now being sent to the database.

 

The big problem in the first place was the forms function page which I was forgetting about when I first added the radio button.

 

I have been checking around and there doesn't seem to be any way to make the checkbox bigger but that is a problem for another day.

 

Debugging methods are really the way to go and something that I usually don't want to take the time to use but in the end would save time.

Share this post


Link to post
Share on other sites

No one "likes" debugging, but it's an essential part of coding for everyone, so get used to it.

 

Also, you are correct in that checkboxes cannot really be styled.

If you want custom ("nicer") checkboxes, you'll have to go with a JS solution which means attaching onclick events or the like to graphics or styled divs, etc. to make bigger/more appealing "checkboxes".

 

Note that if you go that route though, you should still have a traditional fallback for non-JS-enabled browsers.

Share this post


Link to post
Share on other sites

I have revisited this and decided that my users HAVE to agree to the Terms and Conditions on my Registration page so my check box is now preselected.

 

The following is what I now have at the top of the page -

 

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Check for an agreement:
	$agree = 'Agree';

Then where the form is located I have the following -

 

<label for="agree"></label>
<input type="checkbox" name=mybox value="1" checked>
I agree to the user Terms and Conditions.

A slight problem could present itself if someone decides to "uncheck" the box. The form still goes through and the word "Agree" goes into the database, however, the person may think that they have NOT agreed to the Terms and Conditions.

Share this post


Link to post
Share on other sites

Marie, your input must have an ID value equivalent to the value of the for attribute of the corresponding label element.

In the above case, the input must have an ID of "agree" (and no other elements on the page should have that ID).

 

Also, you shouldn't gamble on users deselecting the check box and assuming they agree anyway.

You should properly check their selection, and spit out a message telling them they must agree to proceed in the event that they uncheck the check box.

Share this post


Link to post
Share on other sites

I agree with what you say above. I was having trouble with the way it was set up previously.

 

This is what I had in the top part of the page which was previously supplied by Margaux.

 

    // Check for an agreement:
	if (isset($_POST['agree']) && ($_POST['agree'] == TRUE)) {
	$agree = 'Agree';
	} else {
    $reg_errors['agree'] = 'Please agree to the user Terms and Conditions.';
    }

This only worked once. So if a registrant filled in all the fields except a few of them - including the checkbox - they would get a prompt to fill in the checkbox as well as the other fields that were missed. However, IF they missed the checkbox again and maybe one of the other fields they would not be prompted again to fill out the checkbox. It would just be blank and if any of the other fields were not filled out, they would receive the prompt for those fields.

 

So I decided to rework things slightly.

 

This is what I previously had in the form.

 

		<label for="agree"></label>
        <p class="noticeType">I agree to the user Terms and Conditions.
		<?php create_form_input('agree', 'checkbox', $reg_errors); ?> </p> 

This was picking up the checkbox from the forms function file. I was thinking of changing this file to include preselected checkboxes but what if I don't want preselected checkboxes in any of my other pages?

 

So what exactly will an ID value do for me at this point?

 

Thanks for your help.

Share this post


Link to post
Share on other sites

So what exactly will an ID value do for me at this point?

your input must have an ID value equivalent to the value of the for attribute of the corresponding label element.

It is how the label knows which input element it is the label for.

 

This was picking up the checkbox from the forms function file. I was thinking of changing this file to include preselected checkboxes but what if I don't want preselected checkboxes in any of my other pages?

In your create_input_function you could single out the agree checkbox and make it sticky, something like

if ($type == 'checkbox' && $name='agree') {echo '<input type="checkbox" name="agree" id="agree"'; 
   if ($value) echo 'checked';
   
   if (array_key_exists($name,$errors)) {
      echo '<span class="error">' . $errors[$name] . '</span>';
   }
   else {
      echo '/>';
   }
   
}

Not quite sure what your create_form_input() function looks like but hopefully this will give you some ideas. I've not made full use of the variables but I'm sure you get the general idea.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...