Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Form input radio checked by default.

Posted on 2008-10-07
4
Medium Priority
?
2,258 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
[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
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1000 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 1000 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

Industry Leaders: 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 dynamically set the form action using jQuery.

688 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