Solved

PHP Form input radio checked by default.

Posted on 2008-10-07
4
2,243 Views
Last Modified: 2013-12-12
I have a set of 3 radio buttons with this coding.  I need the first one (RESI) to be checked by default.  Please advise how to do this, I keep screwing up the syntax.

      <input type="radio" name="searchTable" id="searchTable" <?php if (isset($_SESSION['searchTable']) && strlen($_SESSION['searchTable'])>0 && strcmp($_SESSION['searchTable'],"RESI")==0) echo " checked='checked'"; ?> value="RESI" />
    </label>
      Homes
      <input type="radio" name="searchTable" id="searchTable2" <?php if (isset($_SESSION['searchTable']) && strlen($_SESSION['searchTable'])>0 && strcmp($_SESSION['searchTable'],"RNTL")==0) echo " checked='checked'"; ?> value="RNTL" />
      Rentals
      <input type="radio" name="searchTable" id="searchTable3" <?php if (isset($_SESSION['searchTable']) && strlen($_SESSION['searchTable'])>0 && strcmp($_SESSION['searchTable'],"LOTL")==0) echo " checked='checked'"; ?> value="LOTL" />
      Lot
0
Comment
Question by:pda4me
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 22663968
try:
<input type="radio" name="searchTable" id="searchTable" <?php if (isset($_SESSION['searchTable']) && strval($_SESSION['searchTable'])!="RNTL" && strval($_SESSION['searchTable'])!="LOTL") echo " checked='checked'"; ?> value="RESI" />
    </label>
      Homes 
      <input type="radio" name="searchTable" id="searchTable2" <?php if (isset($_SESSION['searchTable']) && strlen($_SESSION['searchTable'])>0 && strcmp($_SESSION['searchTable'],"RNTL")==0) echo " checked='checked'"; ?> value="RNTL" />
      Rentals 
      <input type="radio" name="searchTable" id="searchTable3" <?php if (isset($_SESSION['searchTable']) && strlen($_SESSION['searchTable'])>0 && strcmp($_SESSION['searchTable'],"LOTL")==0) echo " checked='checked'"; ?> value="LOTL" />

Open in new window

0
 
LVL 14

Assisted Solution

by:yessirnosir
yessirnosir earned 250 total points
ID: 22666041
I tested the original code and it seems to work just fine as is.  The only thing I added was a single line to set the session variable 'searchTable' to RESI.  The form came up with the first radio button selected.  So not sure why you are having a problem....   is that session variable getting set properly by the rest of your code?

<?php $_SESSION['searchTable']="RESI"; ?>
<input type="radio" name="searchTable" id="searchTable" <?php if (isset($_SESSION['searchTable']) && strlen($_SESSION['searchTable'])>0 && strcmp($_SESSION['searchTable'],"RESI")==0) echo " checked='checked'"; ?> value="RESI" />
    </label>
      Homes
      <input type="radio" name="searchTable" id="searchTable2" <?php if (isset($_SESSION['searchTable']) && strlen($_SESSION['searchTable'])>0 && strcmp($_SESSION['searchTable'],"RNTL")==0) echo " checked='checked'"; ?> value="RNTL" />
      Rentals
      <input type="radio" name="searchTable" id="searchTable3" <?php if (isset($_SESSION['searchTable']) && strlen($_SESSION['searchTable'])>0 && strcmp($_SESSION['searchTable'],"LOTL")==0) echo " checked='checked'"; ?> value="LOTL" />
      Lot

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22668812
>>I tested the original code and it seems to work just fine as is. The only thing I added was a single line to set the session variable 'searchTable' to RESI...
Which is NOT the correct approach. If you set the session variable on pageA.php to RNTL and then are redirected to the page that contains the code above, you suggestion will ALWAYS make RESI the checked item because you are overwriting the session and will loose the RNTL option. It should default to RESI only is session is NOT one of the other options
0
 
LVL 14

Expert Comment

by:yessirnosir
ID: 22671294
whoaaa Hielo!  I think you've misunderstood... I wasn't suggesting that OP use that line to force the session variable in HIS code... I just added that line to temporarily force it to a known value for the purpose of testing the rest of the code..  that allowed me to prove that there was no SYNTAX error in the code the OP posted.  This suggests that the OP's problem may be something to do with the way his code sets the session variable, so he probably needs to check how that code works.

re:  "It should default to RESI only if session is NOT one of the other options", I think you are speculating about how the OP wants the LOGIC of the code to work, so your proposed modification to his logic may or may not be helpful.  As originally written, his logic handles properly the three cases of session variable being set to RESI, RNTL, and LOTL.  If he wants the code to handle situations when the variable is set to some other value, then your proposed change would help.  If he wants the code to handle situations where the session variable hasn't been set yet, then he would need further changes to the logical structure.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article discusses four methods for overlaying images in a container on a web page
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…

821 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