/    Sign up×
Community /Pin to ProfileBookmark

Validate a checkbox

in html:

[code=html]<input type=”checkbox” name=”agree2terms” value=”<?=$agree2terms?>”/>[/code]

in php:

[code=php]<?php
$agree2terms = ”; // terms and conditions
[/code]

I don’t know how to valide if the checkbox is checked :p

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@TheRaveDec 18.2007 — The $_POST['agree2terms'] will return the string in the html 'value' field if the checkbox is checked.
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorDec 18.2007 — I copied the validation of a text box we have and I believe it works :p

[code=php]
if(empty($agree2terms)):
$messages .= 'ERROR: Place a checkmark on TERMS & CONDITIONS<br>'.$lf;
endif;
[/code]


I am new at this and thought that since it's a checkbox, the validation would be different than a textbox.

Thank you so much.
Copy linkTweet thisAlerts:
@felgallDec 18.2007 — A checkbox can only have one value - the value assigned to it. If it isn't checked then the field doesn't exist at all and so is not empty or any other value. You validate a checkbox by testing if the field exists.

The alternative is to create a hidden field with the same name and a different value directly before the checkbox. If you do that then you get a field that will always exist and have one of two values - the value of the hidden field if the checkbox is not checked or the value of the checkbox if it is.
Copy linkTweet thisAlerts:
@xoxLISAxoxauthorDec 18.2007 — Dear Felgall:

As I said earlier, I am new at this and trying to learn as quick as I can so we can go live. Below is the html code for the checkbox:

[code=html]
<input type="checkbox" " name="agree2terms" value="<$agree2terms?>"/>
[/code]


The value is declared at the beginning of the php file, and I just copied the validation from a text box and somehow it seems to work.

Is this wrong?
×

Success!

Help @xoxLISAxox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...