Radio Buttons answers not going into MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • help4me
    New Member
    • Aug 2007
    • 18

    Radio Buttons answers not going into MySQL

    Hope someone can help me out with this.
    I'm having trouble getting my radio button values into a MySQL database. And after I do get them inserted I'm going to want to be able to show them back in the form again (if the respondent didn't finish the whole form the first time). My text and textarea fields are inserting just fine and I'm able to bring those back up too. It's just the radio buttons that aren't working. When I created the table I used VARCHAR. Was that even right?


    These are the radio buttons in my form:

    [HTML]
    1st Question:
    <p><input type="radio" name="QA" value="AA"> 1st Choice</p>
    <p><input type="radio" name="QA" value="AB"> 2nd Choice</p>
    <p><input type="radio" name="QA" value="AC"> 3rd Choice</p>
    <
    <p></p>

    <p>2nd Question:</p>
    <p><input type="radio" name="QB" value="BA"> 1st Choice</p>
    <p><input type="radio" name="QB" value="BB"> 2nd Choice</p>
    <p><input type="radio" name="QB" value="BC"> 3rd Choice</p>[/HTML]

    These are the instructions on the page where I'm updating them into a table called "XXX". $_SESSION['passcode' comes from a signup page. On that page I've got the INSERT code to get their name and other info. I also insert blank spaces for all the other fields I'll be needing.


    [PHP]$AA = $_POST['AA'];
    $AB = $_POST['AB'];
    $AC = $_POST['AC'];
    $BA = $_POST['BA'];
    $BB = $_POST['BB'];
    $BC = $_POST['BC'];


    mysql_query("UP DATE XXX SET AA='$AA', AB='$AB', AC='$AC', BA='$BA', BB='$BB', BC='$BC' WHERE passcode='{$_SE SSION['passcode']}'")
    or die(mysql_error ()); [/PHP]

    I'm not sure how to select the radio buttons because I haven't been able to test it since I can't get the data into the database.
    Many thanks.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    I think you are misunderstandin g how radio buttons work in HTML.

    Using your form, even tho you have 6 buttons, only 2 values will be sent.
    Only a single radio button out of every radio button in the form sharing a name will be used. So for this form, a value for 'QA' and 'QB' will be sent, each of those containing the value of the selected radio button.

    If you want a specific radio button to be selected by default, you should be able to simply add "selected=t rue" to the <input> tag.

    Comment

    • help4me
      New Member
      • Aug 2007
      • 18

      #3
      Thanks for responding. I've worked at it and now have the values going to my database. Now I need to pull them back into my form when I open it again. I tried the following but it isn't working.



      [PHP]
      switch($QA)
      {
      case "QA": $checkAA = "checked"; break;
      case "QA": $checkAB = "checked"; break;
      case "QA": $checkAC = "checked"; break;
      }

      switch($QB)
      {
      case "QB": $checkBA = "checked"; break;
      case "QB": $checkBB = "checked"; break;
      case "QB": $checkBC = "checked"; break;
      } [/PHP]


      [HTML]
      1st Question:
      <p><input type="radio" name="QA" value="AA" <?=$checkAA?> > 1st Choice</p>
      <p><input type="radio" name="QA" value="AB" <?=$checkAB?> > 2nd Choice</p>
      <p><input type="radio" name="QA" value="AC" <?=$checkAC?> > 3rd Choice</p>

      <p></p>

      <p>2nd Question:</p>
      <p><input type="radio" name="QB" value="BA" <?=$checkBA?> > 1st Choice</p>
      <p><input type="radio" name="QB" value="BB" <?=$chechBB?> > 2nd Choice</p>
      <p><input type="radio" name="QB" value="BC" <?=$checkBC?> > 3rd Choice</p>[/HTML]

      Any hints you can give?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        Check out the values you use in your switch statements. You are using the same values for all cases in both of them ("QA" and "QB"). Which probably means that AA and BA will always be selected.

        Also, just a note, the <?=$var?> syntax is very dangerous. It's a part of the short-tag deal (<?..?>) which is disabled by default so it's very probable that you will run into problems if you try to use this code on any other server than the one you develop this on.
        I'd recommend always using <?php...?>.
        Replacing <?=$var?> with <?php echo $var; ?>

        Comment

        • help4me
          New Member
          • Aug 2007
          • 18

          #5
          Hi Atli,
          I still can't get it. This is want I have now. (Just the 1st question)

          [PHP]switch($QA)
          {
          case "AA": $checkAA = "checked"; break;
          case "AB": $checkAB = "checked"; break;
          case "AC": $checkAC = "checked"; break;

          }[/PHP]

          [HTML]1st Question:
          <p><input type="radio" name="QA" value="AA" <?php echo $checkAA;?>> 1st Choice</p>
          <p><input type="radio" name="QA" value="AB" <?php echo $checkAB;?>> 2nd Choice</p>
          <p><input type="radio" name="QA" value="AC" <?php echo $checkAC;?>> 3rd Choice</p>
          [/HTML]

          It is posting to the database OK, but when I bring up the form again, nothing is checked. I've even tried this:


          [HTML]<input type="radio" name="QA" value="AA" <?php echo ($QA=='AA')? 'checked' : '' ?> >1st Choice
          <input type="radio" name="QA" value="AB" <?php echo ($QA=='AB')? 'checked' : '' ?> >2nd Choice
          <input type="radio" name="QA" value="AC" <?php echo ($QA=='AC')? 'checked' : '' ?> >3rd Choice[/HTML]

          No luck. I worked on this all day. What am I doing wrong?

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Hi.

            Thats strange. Given that the $QA variable contains a value that is either 'AA', 'AB' or 'AC', both of your code snippets should work.

            I did a little test on this myself, worked perfectly:
            [code=php]
            <?php
            $QA = $_POST['QA'];
            ?>
            <form action="?" method="POST">
            <input type="radio" name="QA" value="AA" <?php if($QA=='AA')ec ho "checked"; ?> /> AA <br />
            <input type="radio" name="QA" value="AB" <?php if($QA=='AB')ec ho "checked"; ?> /> AB <br />
            <input type="radio" name="QA" value="AC" <?php if($QA=='AC')ec ho "checked"; ?> /> AC <br />
            <input type="submit" />
            </form>
            [/code]

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              I think you are trying to create a sticky form.
              You are close but need to read the $_POST variables.
              I assume you are POSTing a form
              ie
              [PHP]<input type="radio" name="QA" value="AA" <?php if(isset($_POST['AA']))
              echo 'checked'; ?> >1st Choice[/PHP]

              Comment

              • help4me
                New Member
                • Aug 2007
                • 18

                #8
                I finally got it to work using this:

                [PHP]<input type="radio" name="QA" value="AA" <?php if($row[QA]=='AA')echo "checked"; ?> /> AA <br />[/PHP]
                Since I'm pulling it from a certain row in my database I guess I needed the $row[].

                Thanks for the help!

                Comment

                Working...