Solved

using php to set form values

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and 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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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