Solved

using php to set form values

Posted on 2009-06-26
3
1,343 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now