Solved

using php to set form values

Posted on 2009-06-26
3
1,357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

730 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