/    Sign up×
Community /Pin to ProfileBookmark

Database value to se checkbox

When retrieving values from a database to display on a form I want to “check” a checkbox based on the value of one of the retrieved form text fields. For example something like this:

<?
if ($textfield1 = ‘abc’) {
$checkbox=’checked’;
?>

but I don’t think this works !!

I have a javascript that will change the text field value to ‘abc’ when checked (and to blank when unchecked) – however I just need the PHP code to set the checkbox when the form is first loaded with retrieved database values. I don’t wish to store the checkbox value in the database – just the text value.

Any suggestions please !!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 03.2005 — [code=php]
<?php
$checked = "";
if($textfield1 == "abc")
{
$checked = " checked";
}
echo "<p><input name='mycheckbox' value='abc'$checked> abc</p>n";
?>
[/code]
Copy linkTweet thisAlerts:
@jdorfJan 03.2005 — [code=php]
echo "<input type='checkbox'";
if ($textfield1 = 'abc') {
echo " CHECKED";
}
echo " />n";
[/code]


(you can do the same with the text box too...
Copy linkTweet thisAlerts:
@paulbauthorJan 03.2005 — Thanks for these replies - they look like they should work.

However not being an experienced PHP programmer I am struggling to understand where the code goes. Is is separate stand alone code or should it go in my checkbox form code which currently is:

<input type="checkbox" name="mycheckbox" onclick="xyz()">

(xyz is the name of my javascript function that changes the text field value when checked or unchecked)

I don't see how to do this - appreciate your patience with me !!
Copy linkTweet thisAlerts:
@NogDogJan 03.2005 — If you want to minimize the changes to your page (not necessarily a good or bad thing), you could...
[code=php]
<input type="checkbox" name="mycheckbox" onclick="xyz()"
<?php
if($textfield1 == "abc")
{
echo " checked";
}
?>
>
[/code]

(Basically, any time you are within a <?php...?> section, that PHP script segement gets parsed and executed, and any output - such as from an "echo" statement - gets output into the HTML text stream at that point in the page.)
Copy linkTweet thisAlerts:
@paulbauthorJan 03.2005 — Thanks so much NogDog - I now understand and it works perfectly !!
×

Success!

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