Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Setting and saving radio values on form

    Hi all,

    Here's a small php file:

    Code:
    <?php # display.php
    
    $ptype = '';
    $parea = '';
    $pcity = '';
     
    
    //Check if the form has been submitted
    if (isset($_POST['submit']))
    {
        
      $type     = $_POST['fr_type'];
      $area     = $_POST['fr_area'];    
      $city     = $_POST['fr_city'];
      
      $ptype = $type;
      $parea = $area;
      $pcity = $city;
    
    } 
    echo '<html>
      <head>
      <title>Display Test</title>
      </head>
      <body>
      <form name="display_form" method="POST" action="display.php"> 
      <p align="center">&nbsp;</p>
        <table border="0" width="930">
          <tr>
            <td width="145"></td>
            <td width="254"><b>Area</b></td>
            <td width="217">
              <select size="1" name="fr_area">
                <option>Urban</option>
                <option>Rural</option>
              <script language=JavaScript>
                document.display_form.fr_area.value = "' .$parea. '";
              </script>
              </select>
            </td>
            <td width="202"></td>
          </tr>
          <tr>
            <td width="145"></td>
            <td width="254"<b>Location</b></td>
            <td width="217">
              <input type="text" name="fr_city" size="20" maxlength="35" value="' .$pcity. '">
            </td>
            <td width="202"></td>
          </tr>
    
    
          <tr>
            <td width="145">&nbsp;</td>
            <td width="254"><b>Type of Accomodations</b></td>
            <td width="217">
              <p align="left">
                Apartment<input type="radio" name="fr_type" value="apartment">
                Loft     <input type="radio" name="fr_type" value="loft">
                House    <input type="radio" name="fr_type" value="house"</p>
            </td>
            <td width="202">&nbsp;</td>
          </tr>
          <tr>
            <td width="145">&nbsp;</td>
            <td width="254"><input type="submit" name="submit" value="List" /></td>
          </tr>
        </table>
      </form>
    
    
      </body>
      </html>
    ';
    ?>
    As you can see it's pretty simple.

    Here's my problem:

    As you can see I 'freeze' the values in the Area and Location fields are the submit button as been pushed by using the value from variables in the PHP section of the source code. What I can't freeze is the value of the radio button. What I want is for the value of the radio button to be displayed, just like the 'text' and 'select' types.

    After the 'List' button is pressed, the radio value is cleared!
    AAAHHHH!!!

    There has to be a snippet of Javascript that can fix this problem, I'm just to dumb to figure it out.

    Thanks in advance.

    Mike in Toronto

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    This is a pretty good time to use the alternate "if" statement syntax. The syntax is:

    (condition) ? true : false

    You would use it like this:
    PHP Code:
    echo ($_POST['fr_type'] == 'apartment') ? 'selected' ''
    You need to determine which radio button contains the value of $_POST['fr_type'], and when there's a match, the word "selected" needs to be inserted into the form element. You can do it like this:

    PHP Code:
               Apartment<input type="radio" name="fr_type" value="apartment" <?php echo ($_POST['fr_type'] == 'apartment') ? 'selected' ''?>>
                Loft     <input type="radio" name="fr_type" value="loft" <?php echo ($_POST['fr_type'] == 'loft') ? 'selected' ''?>>
                House    <input type="radio" name="fr_type" value="house" <?php echo ($_POST['fr_type'] == 'house') ? 'selected' ''?>></p>

  • #3
    New Coder
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the advise.

    I modified the statement but it still doesn't work. The big problem is that the entire HTML portion of the code is within a PHP echo statement.
    Code:
    <?php # display.php
    
    $ptype = '';
    $parea = '';
    $pcity = '';
     
    
    //Check if the form has been submitted
    if (isset($_POST['submit']))
    {
        
      $type     = $_POST['fr_type'];
      $area     = $_POST['fr_area'];    
      $city     = $_POST['fr_city'];
      
      $ptype = $type;
      $parea = $area;
      $pcity = $city;
    
    }
    echo '
    <html>
      <head>
      <title>Display Test</title>
      </head>
      <body>
      <form name="display_form" method="POST" action="display.php"> 
      <p align="center">&nbsp;</p>
        <table border="0">
          <tr>
            <td width="145"></td>
            <td width="254"><b>Area</b></td>
            <td width="217">
              <select size="1" name="fr_area">
                <option>Urban</option>
                <option>Rural</option>
              <script language=JavaScript>
                document.display_form.fr_area.value ="' .$parea. '";
              </script>
              </select>
            </td>
            <td width="202"></td>
          </tr>
          <tr>
            <td width="145"></td>
            <td width="254"<b>Location</b></td>
            <td width="217">
              <input type="text" name="fr_city" size="20" maxlength="35" value="' .$pcity. '">
            </td>
            <td width="202"></td>
          </tr>
    
    
          <tr>
            <td width="145">&nbsp;</td>
            <td width="254"><b>Type of Accomodations</b></td>
            <td width="217">
              <p align="left">
                Apartment<input type="radio" name="fr_type" value="apartment" <?php echo ($_POST["fr_type"] == "apartment") ? "selected" : ""; ?>>
                Loft     <input type="radio" name="fr_type" value="loft"      <?php echo ($_POST["fr_type"] == "loft") ? "selected" : ""; ?>>
                House    <input type="radio" name="fr_type" value="house"     <?php echo ($_POST["fr_type"] == "house") ? "selected" : ""; ?>></p>
            </td>
            <td width="202">&nbsp;</td>
          </tr>
          <tr>
            <td width="145">&nbsp;</td>
            <td width="254"><input type="submit" name="submit" value="List" /></td>
          </tr>
        </table>
      </form>
      </body>
      </html>'
    ;
    ?>
    So I have to convert single quotes to double qoutes which I think is real screwing up the alternative IF statement.

    I believe the alternative IF statement is the correct solution to this problem, but my inexperience with PHP/HTML is causing me problems. Hey, I'm a PL/SQL guy.

    I tried to remove the HTML from the echo statement, but then I have a problem with the other two PHP variables ($parea & $pcity).

    Any suggestions? I'm in a pickle.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You're right, I didn't notice the echo statement. You may want to think about just bailing out of PHP mode to print out all that html mark up. You can echo the PHP variables by using <?php echo $variable; ?> right in the middle of the html mark up-- makes for clearer code imo.

    I see you're moving $_POST variables to work variables; it's really an unnecessary step, but since you have it in there I'll just use them.

    Regarding the <input type="select"> tag, you are imbedding a Javascript command to populate that-- you don't need to do this; you can throw some PHP in there, similar to the techniqe used on the radio buttons. See code below:

    PHP Code:
    <?php # display.php

    $ptype '';
    $parea '';
    $pcity '';
     

    //Check if the form has been submitted
    if (isset($_POST['submit']))
    {
        
      
    $type     $_POST['fr_type'];
      
    $area     $_POST['fr_area'];    
      
    $city     $_POST['fr_city'];
      
      
    $ptype $type;
      
    $parea $area;
      
    $pcity $city;

    }
    ?> //break out of PHP mode to output the html mark up

    <html>
      <head>
      <title>Display Test</title>
      </head>
      <body>
      <form name="display_form" method="POST" action="display.php"> 
      <p align="center">&nbsp;</p>
        <table border="0">
          <tr>
            <td width="145"></td>
            <td width="254"><b>Area</b></td>
            <td width="217">
              <select size="1" name="fr_area">
                <option <?php echo ($parea == 'Urban') ? 'selected' ''?>>Urban</option>
                <option <?php echo ($parea == 'Rural') ? 'selected' ''?>>Rural</option>
              </select>
            </td>
            <td width="202"></td>
          </tr>
          <tr>
            <td width="145"></td>
            <td width="254"<b>Location</b></td>
            <td width="217">
              <input type="text" name="fr_city" size="20" maxlength="35" value="<?php echo $pcity?>">
            </td>
            <td width="202"></td>
          </tr>


          <tr>
            <td width="145">&nbsp;</td>
            <td width="254"><b>Type of Accomodations</b></td>
            <td width="217">
              <p align="left">
    Apartment<input type="radio" name="fr_type" value="apartment" <?php echo ($ptype == 'apartment') ? 'selected' ''?>>
                Loft     <input type="radio" name="fr_type" value="loft" <?php echo ($ptype == 'loft') ? 'selected' ''?>>
                House    <input type="radio" name="fr_type" value="house" <?php echo ($ptype == 'house') ? 'selected' ''?>></p>        </td>
            <td width="202">&nbsp;</td>
          </tr>
          <tr>
            <td width="145">&nbsp;</td>
            <td width="254"><input type="submit" name="submit" value="List" /></td>
          </tr>
        </table>
      </form>
      </body>
      </html>

  • #5
    New Coder
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank-you, Thank-you, Thank-you.

    If works great once I remove the echo statement and change selected to checked.

    The attribute used, with a type=radio, to indicate that the radio button has been selected is 'checked' not 'selected.

    PHP Code:
                Apartment<input type="radio" name="fr_type" value="apartment" <?php echo ($ptype == 'apartment') ? 'checked'''?>>
                Loft     <input type="radio" name="fr_type" value="loft" <?php echo ($ptype == 'loft') ? 'checked' ''?>>
                House    <input type="radio" name="fr_type" value="house" <?php echo ($ptype == 'house') ? 'checked' ''?>></p></td>
    I spend about an hour typing to figure out why the radio button selected did not retain it value after the 'LIST' button had been clicked. LOL!!

    It's doesn't really matter because it new works perfectly.

    As per your suggestion, I'm going to review my PHP and remove redundancies moving $_POST to work variables and unnecessary Javascript).

    Much relieved in Toronto
    Mike

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Whoops sorry about that, yes "checked" it the keyword to use. It's the little details that can bite ya

  • #7
    New Coder
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No worries.

    Honestly, you just pointed me in the right direction.

    I'm just happy that my client is happy.

    Thank-you once again.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •