Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Form input radio checked by default.

Posted on 2008-10-07
4
Medium Priority
?
2,268 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…
Suggested Courses

916 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