Solved

PHP Form input radio checked by default.

Posted on 2008-10-07
4
2,238 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now