/    Sign up×
Community /Pin to ProfileBookmark

Checkbox datas to the data base? help please

Hello,
How to send back datas from a checkbox to the data base????

what is the php code?

tanx?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@wastedSep 17.2008 — <input type="checkbox" value="1" name="chkBox">
[code=php]
<?php
// if the checkbox is checked while posting the form $chkBox will store 1 otherwise it is empty.
$chkBox = $_POST['chkbox'];

if($chkBox != ''){
// use your database insert query here
}
?>[/code]
Copy linkTweet thisAlerts:
@dabensauthorSep 17.2008 — thanx
Copy linkTweet thisAlerts:
@SyCoSep 17.2008 — <input type="checkbox" value="1" name="chkBox">
[code=php]
<?php
// if the checkbox is checked while posting the form $chkBox will store 1 otherwise it is empty.
$chkBox = $_POST['chkbox'];

if($chkBox != ''){
// use your database insert query here
}
?>[/code]
[/QUOTE]


Why rewrite the variable to another one for no reason?

$chkBox is not a POST super global yet it now holds the value of the POST superglobal. If you just use the POST var then you know where it came from.

Also with error handling turned up you'll get an undefined index error first time the page loads or when the form is submitted without the value checked. When a form is submitted and the checkbox was not checked it is not in the POST array so you can just look to see if it is set, then act upon it.
[code=php]
if(isset($_POST['chkBox'])){
// use your database insert query here
}
[/code]

Don't rename POST/GET vars unless you have a good reason.
×

Success!

Help @dabens 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,
)...