Send Radio Button Results to another form page

Discussion in 'PHP' started by imwebdev, Dec 17, 2008.

  1. #1
    Hi There,
    I am having an issue sending my radio button information to another page of the form!

    I have a form where the user inputs info, once they hit next, they should be taken to page 2 of the form, where elements are prepopulated from the last page.
    I am able to send text box info to my second page with no problems, I can even send the radio button info to the second page, but cannot get it to populate my radio button.

    Here is an example of the code I have

    page1.php
    <form Name="form" method="POST" action="page2.php" >
    <br>
    <b>When do you want to begin?&nbsp;*</b><span>
    <input name=begin type=radio class=mainForm id=begin value="Immediately" checked />
    <label>Immediately</label>
    <input class=mainForm type=radio name=begin id=begin value="1-6 months" />
    <label>1-6 months</label>
    <input class=mainForm type=radio name=begin id=begin value="Don't Know" />
    <label>Don't Know</label></span><br />
    <br>
        
    <b>Best Time to Call&nbsp;*</b><span>
    <input name=call type=radio class=mainForm id=call value="Morning" checked />
    <label>Morning</label>
    <input class=mainForm type=radio name=call id=call value="Evening " />
    <label>Evening </label>
    <input class=mainForm type=radio name=call id=call value="Anytime" />
    <label>Anytime</label>
    </span><br />
    
    <br>	<Input Name="nextButton" Value="Next" Type="submit" />
    </form>
    
    Code (markup):
    page2.php
    
    <?php 
    $sbegin = $_POST['begin'];
    $scall = $_POST['call'];
    
    ?>
    <form Name="form" method="POST" action="page2.php" >
    <b>When do you want to begin?&nbsp;*</b><span>
    <input name=begin type=radio class=mainForm id=begin value="Immediately" checked />
    <label>Immediately</label>
    <input class=mainForm type=radio name=begin id=begin value="1-6 months" />
    <label>1-6 months</label>
    <input class=mainForm type=radio name=begin id=begin value="Don't Know" />
    <label>Don't Know</label></span><br />
    <br>
        
    <b>Best Time to Call&nbsp;*</b><span>
    <input name=call type=radio class=mainForm id=call value="Morning" checked />
    <label>Morning</label>
    <input class=mainForm type=radio name=call id=call value="Evening " />
    <label>Evening </label>
    <input class=mainForm type=radio name=call id=call value="Anytime" />
    <label>Anytime</label>
    </span>
    <p><br>
      Other info.......</p>
    <p><br />
      <Input Name="submitButton" Value="Submit" Type="submit" />
    </p>
    </form>
    
    Code (markup):
    I have a lot more irrelavant code that I have not posted here.
    But I just need to know how I can take the value I have captured from the previous page and get it to populate the radio button on page 2 with the result from page 1.

    Thanks in advance
     
    imwebdev, Dec 17, 2008 IP
  2. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    To populate a radio button you need to specify checked within the declaration.
    <input name=call type=radio class=mainForm id=call value="Morning" checked />
    Code (markup):
    Just like you have there but as you're using XHTML (ends in " />" rather than ">") you need to change the line to read like this:
    <input name="call" type="radio" class="mainForm" id="call" value="Morning" checked="checked" />
    Code (markup):
    Note the addition of double quotes as well!

    If a radio button is or isn't selected all depends on the presence of checked="checked" and we can add that by reading the state of the checkbox from the previous page with this:
    $chk=$_POST['call'];
    PHP:
    Adding that into our XHTML for the radio button we get this:
    <input name="call" type="radio" class="mainForm" id="call" value="Morning" <?=($chk ? 'checked="checked" ' : '')?>/>
    Code (markup):
    Note the space after the closing double quote after the checked bit - this is to keep the XHTML tidy and leave a space before the ending slash.

    If you find problems persisting it might be because you have instances where ID and NAME are called "call" together - I'd change one to be safe and to help you read the page at a later date.
     
    Yesideez, Dec 17, 2008 IP
  3. imwebdev

    imwebdev Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi There,
    Thanks for the post.
    I must be doing something wrong because now all my radio buttons are blank when I pass the values to the page.

    I have checked to see if the checked value of each radio button is being passed to the form, and the values are being passed without a problem.
    I have also renamed the ID of each of the radio buttons so there is no confusion.
    Any other ideas? I am going to try a few things now.

    Thanks
     
    imwebdev, Dec 17, 2008 IP