/    Sign up×
Community /Pin to ProfileBookmark

Checkbox validation

Do you know how to validate checkboxes in PHP so that the user can only tick a limited number?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bluestarsJul 04.2007 — [code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>PHP Test</title>
<link rel="stylesheet" type="text/css" href="/main.css">
<link rel="shortcut icon" href="/favicon.ico">
</head><body>
<?php
if(isset($_POST) && count($_POST)) {
$count = 0;
foreach($_POST as $key => $value) {
if($value == "on")
$count++;
}
echo "<p>$count</p>";
}
else {
?>
<form method="post">
<input type="checkbox" name="cb1" /><br>
<input type="checkbox" name="cb2" /><br>
<input type="checkbox" name="cb3" /><br>
<input type="submit" />
</form>

<?php
}
?>

</body></html>[/code]

That'll work as long as "on" isn't a valid response to one of your other form elements.
Copy linkTweet thisAlerts:
@JavajoobauthorJul 04.2007 — Ah thanks and the $count bit is how much you limit it two?
Copy linkTweet thisAlerts:
@bluestarsJul 04.2007 — All my code does is count it for you. After that, you could put an if statement comparing $count to your value, then kick it back to the user if it was greater.

You have any programming experience?
Copy linkTweet thisAlerts:
@JavajoobauthorJul 04.2007 — I tried this in Javascript but then I realised that it can be bypassed so I thought I should do it in PHP
×

Success!

Help @Javajoob 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...