Solved

using php to set form values

Posted on 2009-06-26
3
1,355 Views
Last Modified: 2012-05-07
I have a php file that includes a form with checkboxes and other fields. I need a way to use php to check the boxes based on variables. I do this with lists using the following:

<option value="Houses" <?php if ($buildingtype=="Houses") echo "selected";?>Houses</option>

I need something equivalent for checkboxes. Here is what I have tried (and what does not work):

<input name="Sales" type="checkbox" value="Yes" <?php if ($sales=="Yes") echo "checked";?>>Sales
0
Comment
Question by:karimjohnson
3 Comments
 
LVL 50

Accepted Solution

by:
Steve Bink earned 100 total points
ID: 24726403
<input name="Sales" type="checkbox" value="Yes"<?php if ($sales=="Yes") echo " checked=\"checked\"";?>>Sales
0
 
LVL 3

Assisted Solution

by:JPM
JPM earned 25 total points
ID: 24726446
Instead of escaping the " , you might mix .
ex :

<input name="Sales" type="checkbox" value="Yes" <?php if ($sales=='Yes') echo 'checked'; ?> >Sales

With PHP, you can mix '   and ".
Especially useful with   echo.
Ex :     echo " bla bla 'foo' ";        OR    echo ' bla bla "foo" ';

HTWH.
0
 
LVL 4

Expert Comment

by:termlimit
ID: 24732618
since you are not dealing with any variables in your string PHP is actually faster if you do the following:
<input name="Sales" type="checkbox" value="Yes"<?php if ($sales=="Yes") echo ' checked="checked"'; ?> />Sales

Only outputting 'checked' doesn't make the form get checked.  You need checked="checked" to make it work.

The best way to make this work is to actually create a simple class that handles outputting all of your form elements.  This way you can simply call a static method in the class to output your checkbox.  This method is a lot easier to work with and less hassle overall.
Class HTML {
 
     public static function checkbox($name, $value, $checked = false, $extra) {
         // Here you build the output of the form field you need.
     }
}

Open in new window

0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question