Solved

PHP Form input radio checked by default.

Posted on 2008-10-07
4
2,246 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

617 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