Dynamic Radio Buttons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerim79

    Dynamic Radio Buttons

    My situation is that I have a form that asks the user for a number.
    Next, I execute a while loop that displays a group of questions the
    amount of times the customer entered. For instance, the loop looks
    this:

    while ($Number!=0){
    <input type="radio" name="Age[]" value="20-30">20-30
    <input type="radio name="Age[]" value="30-40">30-40
    <input type="radio name"Age[]" value="40-50">40-50
    $Number--;
    }

    Let's say someone entered 3 for $Number. When the loop executes it
    will produce:

    echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
    <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

    echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
    <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

    echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
    <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

    The problem is that with radio buttons, you can only choose one
    option. With this code, it only allows the user to choose one option
    from among the 9 listed, instead of one from each set. I am trying to
    find a way to dynamically name each set. If I use checkboxes, then it
    records the information correctly and correctly writes it to the
    database. However, checkboxes don't look good for this application in
    my opinion, and there is no way for me to keep someone from checking
    more than one box, that I know of. Here is the PHP code I am using to
    catch the user's input and write it to a database:

    $Age = $_POST['Age'];

    for ($i=0; $i < $Number; $i++) {
    $query="INSERT INTO table VALUES('$Age[$i])";
    $result = mysql_query($qu ery) or die('Query failed: ' .
    mysql_error());
    }

    Like I say, that works fine if I use checkboxes, without changing the
    HTML name="Age[]". I am looking for a way to define "sets" of radio
    buttons with the same name, so that only one from within a set can be
    chosen at a time. Is there something such as:

    echo "<group name="1">
    <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
    <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
    </group>";

    echo "<group name=\"2\">
    <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
    <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
    </group>";

    echo "<group name=\"3\">
    <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
    <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
    </group>";

    This may be more of a HTML question, but I am open to any solution
    that would help. Any way to use PHP to accomplish my goal? Even if
    there is a way to dynamically name each set such as:

    echo "while ($Number!=0){
    <input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
    <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age$Numb er[]\" value=\"40-50\">40-50
    $Number--
    } ";

    I would just have to create a loop to run through each array when
    writing to the database, but that shouldn't be a problem.

  • Moot

    #2
    Re: Dynamic Radio Buttons

    On Apr 6, 9:36 am, "Jerim79" <m...@hotmail.c omwrote:
    >
    echo "while ($Number!=0){
    <input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
    <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age$Numb er[]\" value=\"40-50\">40-50
    $Number--
    } ";
    >
    You just answered your own question. Well, almost. Syntax is a
    little off, but you were on the right track.

    while ($Number!=0){
    echo "<input type=\"radio\" name=\"Age{$Num ber}[]\" value=
    \"20-30\">20-30
    <input type=\"radio\" name=\"Age{$Num ber}[]\" value=\"30-40\">30-40
    <input type=\"radio\" name=\"Age{$Num ber}[]\" value=\"40-50\">40-50";
    $Number--;
    }


    Comment

    • Erwin Moller

      #3
      Re: Dynamic Radio Buttons

      Jerim79 wrote:
      My situation is that I have a form that asks the user for a number.
      Next, I execute a while loop that displays a group of questions the
      amount of times the customer entered. For instance, the loop looks
      this:
      >
      while ($Number!=0){
      <input type="radio" name="Age[]" value="20-30">20-30
      <input type="radio name="Age[]" value="30-40">30-40
      <input type="radio name"Age[]" value="40-50">40-50
      $Number--;
      }
      >
      Let's say someone entered 3 for $Number. When the loop executes it
      will produce:
      >
      echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
      <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
      <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
      >
      echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
      <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
      <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
      >
      echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
      <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
      <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
      >
      The problem is that with radio buttons, you can only choose one
      option. With this code, it only allows the user to choose one option
      from among the 9 listed, instead of one from each set. I am trying to
      find a way to dynamically name each set. If I use checkboxes, then it
      records the information correctly and correctly writes it to the
      database. However, checkboxes don't look good for this application in
      my opinion, and there is no way for me to keep someone from checking
      more than one box, that I know of. Here is the PHP code I am using to
      catch the user's input and write it to a database:
      >
      $Age = $_POST['Age'];
      >
      for ($i=0; $i < $Number; $i++) {
      $query="INSERT INTO table VALUES('$Age[$i])";
      $result = mysql_query($qu ery) or die('Query failed: ' .
      mysql_error());
      }
      >
      Like I say, that works fine if I use checkboxes, without changing the
      HTML name="Age[]". I am looking for a way to define "sets" of radio
      buttons with the same name, so that only one from within a set can be
      chosen at a time. Is there something such as:
      >
      echo "<group name="1">
      <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
      <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
      <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
      </group>";
      >
      echo "<group name=\"2\">
      <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
      <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
      <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
      </group>";
      >
      echo "<group name=\"3\">
      <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
      <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
      <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
      </group>";
      >
      This may be more of a HTML question, but I am open to any solution
      that would help. Any way to use PHP to accomplish my goal? Even if
      there is a way to dynamically name each set such as:
      >
      echo "while ($Number!=0){
      <input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
      <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
      <input type=\"radio\" name=\"Age$Numb er[]\" value=\"40-50\">40-50
      $Number--
      } ";
      >
      I would just have to create a loop to run through each array when
      writing to the database, but that shouldn't be a problem.
      Hi,

      You cannot make a group of the radiobuttons since you named them all Age[]
      and that IS the group as far as HTML is concerned.
      So just code it in such a way they have different names, like this:

      <?php
      // receive number
      $number = (int)$_POST["number"];
      ?>
      <input type="hidden" name="numberOfA ges" value="<?php echo $number; ?>">
      <?php
      for ($count=0;$coun t<$number;$coun t++){
      ?>
      <input type="radio" name="Age<?php echo $number; ?>[]"
      value="20-30">20-30
      <input type="radio name="Age[]<?php echo $number; ?>"
      value="30-40">30-40
      <input type="radio name"Age[]<?php echo $number; ?>"
      value="40-50">40-50
      <?php
      }
      ?>


      Now them radiogroups have names like Age0[] and Age1[]

      And in the receiving script:
      $numberOfAges = $_POST["numberOfAg es"];
      for ($count=0;$coun t<$numberOfAges ;$count++){
      $name = "Age".$coun t;
      $theSelectedVal ue = $_POST[$name];
      // Do whatever you want with $theSelectedVal ue
      }


      Not tested, but I hope you get my drift. :-)

      Regards,
      Erwin Moller

      Comment

      • Jerim79

        #4
        Re: Dynamic Radio Buttons

        On Apr 6, 9:08 am, Erwin Moller
        <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
        Jerim79 wrote:
        My situation is that I have a form that asks the user for a number.
        Next, I execute a while loop that displays a group of questions the
        amount of times the customer entered. For instance, the loop looks
        this:
        >
        while ($Number!=0){
        <input type="radio" name="Age[]" value="20-30">20-30
        <input type="radio name="Age[]" value="30-40">30-40
        <input type="radio name"Age[]" value="40-50">40-50
        $Number--;
        }
        >
        Let's say someone entered 3 for $Number. When the loop executes it
        will produce:
        >
        echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
        <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
        <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
        >
        echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
        <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
        <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
        >
        echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
        <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
        <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
        >
        The problem is that with radio buttons, you can only choose one
        option. With this code, it only allows the user to choose one option
        from among the 9 listed, instead of one from each set. I am trying to
        find a way to dynamically name each set. If I use checkboxes, then it
        records the information correctly and correctly writes it to the
        database. However, checkboxes don't look good for this application in
        my opinion, and there is no way for me to keep someone from checking
        more than one box, that I know of. Here is the PHP code I am using to
        catch the user's input and write it to a database:
        >
        $Age = $_POST['Age'];
        >
        for ($i=0; $i < $Number; $i++) {
        $query="INSERT INTO table VALUES('$Age[$i])";
        $result = mysql_query($qu ery) or die('Query failed: ' .
        mysql_error());
        }
        >
        Like I say, that works fine if I use checkboxes, without changing the
        HTML name="Age[]". I am looking for a way to define "sets" of radio
        buttons with the same name, so that only one from within a set can be
        chosen at a time. Is there something such as:
        >
        echo "<group name="1">
        <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
        <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
        <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
        </group>";
        >
        echo "<group name=\"2\">
        <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
        <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
        <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
        </group>";
        >
        echo "<group name=\"3\">
        <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
        <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
        <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
        </group>";
        >
        This may be more of a HTML question, but I am open to any solution
        that would help. Any way to use PHP to accomplish my goal? Even if
        there is a way to dynamically name each set such as:
        >
        echo "while ($Number!=0){
        <input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
        <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
        <input type=\"radio\" name=\"Age$Numb er[]\" value=\"40-50\">40-50
        $Number--
        } ";
        >
        I would just have to create a loop to run through each array when
        writing to the database, but that shouldn't be a problem.
        >
        Hi,
        >
        You cannot make a group of the radiobuttons since you named them all Age[]
        and that IS the group as far as HTML is concerned.
        So just code it in such a way they have different names, like this:
        >
        <?php
        // receive number
        $number = (int)$_POST["number"];
        ?>
        <input type="hidden" name="numberOfA ges" value="<?php echo $number; ?>">
        <?php
        for ($count=0;$coun t<$number;$coun t++){
        ?>
        <input type="radio" name="Age<?php echo $number; ?>[]"
        value="20-30">20-30
        <input type="radio name="Age[]<?php echo $number; ?>"
        value="30-40">30-40
        <input type="radio name"Age[]<?php echo $number; ?>"
        value="40-50">40-50
        <?php
        }
        ?>
        >
        Now them radiogroups have names like Age0[] and Age1[]
        >
        And in the receiving script:
        $numberOfAges = $_POST["numberOfAg es"];
        for ($count=0;$coun t<$numberOfAges ;$count++){
        $name = "Age".$coun t;
        $theSelectedVal ue = $_POST[$name];
        // Do whatever you want with $theSelectedVal ue
        >
        }
        >
        Not tested, but I hope you get my drift. :-)
        >
        Regards,
        Erwin Moller
        Thanks, that did the trick. I was just putting my variable before []
        instead of after. Odd thing is that when I read it into the database,
        I just use $Age[$i], where I thought I would have to use $Age[$i]
        $Number. Not sure what is going on under the hood to make $Age[]
        $Number into just $Age.

        Comment

        • Erwin Moller

          #5
          Re: Dynamic Radio Buttons

          Jerim79 wrote:
          On Apr 6, 9:08 am, Erwin Moller
          <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
          >Jerim79 wrote:
          My situation is that I have a form that asks the user for a number.
          Next, I execute a while loop that displays a group of questions the
          amount of times the customer entered. For instance, the loop looks
          this:
          >>
          while ($Number!=0){
          <input type="radio" name="Age[]" value="20-30">20-30
          <input type="radio name="Age[]" value="30-40">30-40
          <input type="radio name"Age[]" value="40-50">40-50
          $Number--;
          }
          >>
          Let's say someone entered 3 for $Number. When the loop executes it
          will produce:
          >>
          echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
          <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
          <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
          >>
          echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
          <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
          <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
          >>
          echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
          <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
          <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
          >>
          The problem is that with radio buttons, you can only choose one
          option. With this code, it only allows the user to choose one option
          from among the 9 listed, instead of one from each set. I am trying to
          find a way to dynamically name each set. If I use checkboxes, then it
          records the information correctly and correctly writes it to the
          database. However, checkboxes don't look good for this application in
          my opinion, and there is no way for me to keep someone from checking
          more than one box, that I know of. Here is the PHP code I am using to
          catch the user's input and write it to a database:
          >>
          $Age = $_POST['Age'];
          >>
          for ($i=0; $i < $Number; $i++) {
          $query="INSERT INTO table VALUES('$Age[$i])";
          $result = mysql_query($qu ery) or die('Query failed: ' .
          mysql_error());
          }
          >>
          Like I say, that works fine if I use checkboxes, without changing the
          HTML name="Age[]". I am looking for a way to define "sets" of radio
          buttons with the same name, so that only one from within a set can be
          chosen at a time. Is there something such as:
          >>
          echo "<group name="1">
          <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
          <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
          <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
          </group>";
          >>
          echo "<group name=\"2\">
          <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
          <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
          <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
          </group>";
          >>
          echo "<group name=\"3\">
          <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
          <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
          <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
          </group>";
          >>
          This may be more of a HTML question, but I am open to any solution
          that would help. Any way to use PHP to accomplish my goal? Even if
          there is a way to dynamically name each set such as:
          >>
          echo "while ($Number!=0){
          <input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
          <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
          <input type=\"radio\" name=\"Age$Numb er[]\" value=\"40-50\">40-50
          $Number--
          } ";
          >>
          I would just have to create a loop to run through each array when
          writing to the database, but that shouldn't be a problem.
          >>
          >Hi,
          >>
          >You cannot make a group of the radiobuttons since you named them all
          >Age[] and that IS the group as far as HTML is concerned.
          >So just code it in such a way they have different names, like this:
          >>
          ><?php
          >// receive number
          >$number = (int)$_POST["number"];
          >?>
          ><input type="hidden" name="numberOfA ges" value="<?php echo $number; ?>">
          ><?php
          >for ($count=0;$coun t<$number;$coun t++){
          >?>
          > <input type="radio" name="Age<?php echo $number; ?>[]"
          >value="20-30">20-30
          > <input type="radio name="Age[]<?php echo $number; ?>"
          >value="30-40">30-40
          > <input type="radio name"Age[]<?php echo $number; ?>"
          >value="40-50">40-50
          ><?php
          > }
          >?>
          >>
          >Now them radiogroups have names like Age0[] and Age1[]
          >>
          >And in the receiving script:
          >$numberOfAge s = $_POST["numberOfAg es"];
          >for ($count=0;$coun t<$numberOfAges ;$count++){
          > $name = "Age".$coun t;
          > $theSelectedVal ue = $_POST[$name];
          > // Do whatever you want with $theSelectedVal ue
          >>
          >}
          >>
          >Not tested, but I hope you get my drift. :-)
          >>
          >Regards,
          >Erwin Moller
          >
          Thanks, that did the trick. I was just putting my variable before []
          instead of after. Odd thing is that when I read it into the database,
          I just use $Age[$i], where I thought I would have to use $Age[$i]
          $Number. Not sure what is going on under the hood to make $Age[]
          $Number into just $Age.
          Hi Jerim,

          It is just the PHP way of passing arrays around from a form to a receiving
          script.
          If PHP receives for example the following 3 name/value pairs (via POST or
          GET), they are automagically turned into an array:

          1) name: Age[] value: 10
          2) name: Age[] value: 20
          3) name: Age[] value: 30

          If you extract the Age from the posting, like this:
          $passedAge = $_POST["Age"];
          PHP 'sees' that Age[] is posted and extracts all the passed values into an
          array ($passedAge in this case).
          So if a name ends with the [] PHP knows you mean an array.

          It is just the PHP way. :-)

          Consider this wrong example:
          1) name: Age value: 10
          2) name: Age value: 20
          3) name: Age value: 30

          If you try to extract it now like this:
          $passedAge = $_POST["Age"];

          You'll end up with only the last value (30) and not an array.

          So what went wrong in your first try was simply that you didn't end with the
          [].

          Hope that clearifies it a bit.

          Good luck & happy coding!

          Regards,
          Erwin Moller

          Comment

          • bill

            #6
            Re: Dynamic Radio Buttons

            Erwin Moller wrote:
            Jerim79 wrote:
            >
            >On Apr 6, 9:08 am, Erwin Moller
            ><since_humans_ read_this_I_am_ spammed_too_m.. .@spamyourself. comwrote:
            >>Jerim79 wrote:
            >>>My situation is that I have a form that asks the user for a number.
            >>>Next, I execute a while loop that displays a group of questions the
            >>>amount of times the customer entered. For instance, the loop looks
            >>>this:
            >>>while ($Number!=0){
            >>> <input type="radio" name="Age[]" value="20-30">20-30
            >>> <input type="radio name="Age[]" value="30-40">30-40
            >>> <input type="radio name"Age[]" value="40-50">40-50
            >>> $Number--;
            >>> }
            >>>Let's say someone entered 3 for $Number. When the loop executes it
            >>>will produce:
            >>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
            >>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
            >>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
            >>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
            >>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
            >>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
            >>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
            >>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
            >>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
            >>>The problem is that with radio buttons, you can only choose one
            >>>option. With this code, it only allows the user to choose one option
            >>>from among the 9 listed, instead of one from each set. I am trying to
            >>>find a way to dynamically name each set. If I use checkboxes, then it
            >>>records the information correctly and correctly writes it to the
            >>>database. However, checkboxes don't look good for this application in
            >>>my opinion, and there is no way for me to keep someone from checking
            >>>more than one box, that I know of. Here is the PHP code I am using to
            >>>catch the user's input and write it to a database:
            >>>$Age = $_POST['Age'];
            >>>for ($i=0; $i < $Number; $i++) {
            >>> $query="INSERT INTO table VALUES('$Age[$i])";
            >>>$result = mysql_query($qu ery) or die('Query failed: ' .
            >>>mysql_error( ));
            >>>}
            >>>Like I say, that works fine if I use checkboxes, without changing the
            >>>HTML name="Age[]". I am looking for a way to define "sets" of radio
            >>>buttons with the same name, so that only one from within a set can be
            >>>chosen at a time. Is there something such as:
            >>>echo "<group name="1">
            >>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
            >>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
            >>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
            >>></group>";
            >>>echo "<group name=\"2\">
            >>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
            >>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
            >>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
            >>></group>";
            >>>echo "<group name=\"3\">
            >>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
            >>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
            >>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
            >>></group>";
            >>>This may be more of a HTML question, but I am open to any solution
            >>>that would help. Any way to use PHP to accomplish my goal? Even if
            >>>there is a way to dynamically name each set such as:
            >>>echo "while ($Number!=0){
            >>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
            >>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
            >>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"40-50\">40-50
            >>> $Number--
            >>> } ";
            >>>I would just have to create a loop to run through each array when
            >>>writing to the database, but that shouldn't be a problem.
            >>Hi,
            >>>
            >>You cannot make a group of the radiobuttons since you named them all
            >>Age[] and that IS the group as far as HTML is concerned.
            >>So just code it in such a way they have different names, like this:
            >>>
            >><?php
            >>// receive number
            >>$number = (int)$_POST["number"];
            >>?>
            >><input type="hidden" name="numberOfA ges" value="<?php echo $number; ?>">
            >><?php
            >>for ($count=0;$coun t<$number;$coun t++){
            >>?>
            >> <input type="radio" name="Age<?php echo $number; ?>[]"
            >>value="20-30">20-30
            >> <input type="radio name="Age[]<?php echo $number; ?>"
            >>value="30-40">30-40
            >> <input type="radio name"Age[]<?php echo $number; ?>"
            >>value="40-50">40-50
            >><?php
            >> }
            >>?>
            >>>
            >>Now them radiogroups have names like Age0[] and Age1[]
            >>>
            >>And in the receiving script:
            >>$numberOfAg es = $_POST["numberOfAg es"];
            >>for ($count=0;$coun t<$numberOfAges ;$count++){
            >> $name = "Age".$coun t;
            >> $theSelectedVal ue = $_POST[$name];
            >> // Do whatever you want with $theSelectedVal ue
            >>>
            >>}
            >>>
            >>Not tested, but I hope you get my drift. :-)
            >>>
            >>Regards,
            >>Erwin Moller
            >Thanks, that did the trick. I was just putting my variable before []
            >instead of after. Odd thing is that when I read it into the database,
            >I just use $Age[$i], where I thought I would have to use $Age[$i]
            >$Number. Not sure what is going on under the hood to make $Age[]
            >$Number into just $Age.
            >
            Hi Jerim,
            >
            It is just the PHP way of passing arrays around from a form to a receiving
            script.
            If PHP receives for example the following 3 name/value pairs (via POST or
            GET), they are automagically turned into an array:
            >
            1) name: Age[] value: 10
            2) name: Age[] value: 20
            3) name: Age[] value: 30
            >
            If you extract the Age from the posting, like this:
            $passedAge = $_POST["Age"];
            PHP 'sees' that Age[] is posted and extracts all the passed values into an
            array ($passedAge in this case).
            So if a name ends with the [] PHP knows you mean an array.
            >
            It is just the PHP way. :-)
            >
            Consider this wrong example:
            1) name: Age value: 10
            2) name: Age value: 20
            3) name: Age value: 30
            >
            If you try to extract it now like this:
            $passedAge = $_POST["Age"];
            >
            You'll end up with only the last value (30) and not an array.
            >
            So what went wrong in your first try was simply that you didn't end with the
            [].
            >
            Hope that clearifies it a bit.
            >
            Good luck & happy coding!
            >
            Regards,
            Erwin Moller
            Just for clarification:
            ending a passed variable with a [] tells PHP it is an array.
            This is NOT a html construct, html doesn't care.
            Right ?

            bill

            Comment

            • Erwin Moller

              #7
              Re: Dynamic Radio Buttons

              bill wrote:
              Erwin Moller wrote:
              >Jerim79 wrote:
              >>
              >>On Apr 6, 9:08 am, Erwin Moller
              >><since_humans _read_this_I_am _spammed_too_m. ..@spamyourself .comwrote:
              >>>Jerim79 wrote:
              >>>>My situation is that I have a form that asks the user for a number.
              >>>>Next, I execute a while loop that displays a group of questions the
              >>>>amount of times the customer entered. For instance, the loop looks
              >>>>this:
              >>>>while ($Number!=0){
              >>>> <input type="radio" name="Age[]" value="20-30">20-30
              >>>> <input type="radio name="Age[]" value="30-40">30-40
              >>>> <input type="radio name"Age[]" value="40-50">40-50
              >>>> $Number--;
              >>>> }
              >>>>Let's say someone entered 3 for $Number. When the loop executes it
              >>>>will produce:
              >>>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
              >>>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
              >>>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
              >>>>The problem is that with radio buttons, you can only choose one
              >>>>option. With this code, it only allows the user to choose one option
              >>>>from among the 9 listed, instead of one from each set. I am trying to
              >>>>find a way to dynamically name each set. If I use checkboxes, then it
              >>>>records the information correctly and correctly writes it to the
              >>>>database. However, checkboxes don't look good for this application in
              >>>>my opinion, and there is no way for me to keep someone from checking
              >>>>more than one box, that I know of. Here is the PHP code I am using to
              >>>>catch the user's input and write it to a database:
              >>>>$Age = $_POST['Age'];
              >>>>for ($i=0; $i < $Number; $i++) {
              >>>> $query="INSERT INTO table VALUES('$Age[$i])";
              >>>>$result = mysql_query($qu ery) or die('Query failed: ' .
              >>>>mysql_error ());
              >>>>}
              >>>>Like I say, that works fine if I use checkboxes, without changing the
              >>>>HTML name="Age[]". I am looking for a way to define "sets" of radio
              >>>>buttons with the same name, so that only one from within a set can be
              >>>>chosen at a time. Is there something such as:
              >>>>echo "<group name="1">
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
              >>>></group>";
              >>>>echo "<group name=\"2\">
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
              >>>></group>";
              >>>>echo "<group name=\"3\">
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
              >>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
              >>>></group>";
              >>>>This may be more of a HTML question, but I am open to any solution
              >>>>that would help. Any way to use PHP to accomplish my goal? Even if
              >>>>there is a way to dynamically name each set such as:
              >>>>echo "while ($Number!=0){
              >>>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
              >>>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
              >>>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"40-50\">40-50
              >>>> $Number--
              >>>> } ";
              >>>>I would just have to create a loop to run through each array when
              >>>>writing to the database, but that shouldn't be a problem.
              >>>Hi,
              >>>>
              >>>You cannot make a group of the radiobuttons since you named them all
              >>>Age[] and that IS the group as far as HTML is concerned.
              >>>So just code it in such a way they have different names, like this:
              >>>>
              >>><?php
              >>>// receive number
              >>>$number = (int)$_POST["number"];
              >>>?>
              >>><input type="hidden" name="numberOfA ges" value="<?php echo $number;
              >>>?>"<?php
              >>>for ($count=0;$coun t<$number;$coun t++){
              >>>?>
              >>> <input type="radio" name="Age<?php echo $number; ?>[]"
              >>>value="20-30">20-30
              >>> <input type="radio name="Age[]<?php echo $number; ?>"
              >>>value="30-40">30-40
              >>> <input type="radio name"Age[]<?php echo $number; ?>"
              >>>value="40-50">40-50
              >>><?php
              >>> }
              >>>?>
              >>>>
              >>>Now them radiogroups have names like Age0[] and Age1[]
              >>>>
              >>>And in the receiving script:
              >>>$numberOfAge s = $_POST["numberOfAg es"];
              >>>for ($count=0;$coun t<$numberOfAges ;$count++){
              >>> $name = "Age".$coun t;
              >>> $theSelectedVal ue = $_POST[$name];
              >>> // Do whatever you want with $theSelectedVal ue
              >>>>
              >>>}
              >>>>
              >>>Not tested, but I hope you get my drift. :-)
              >>>>
              >>>Regards,
              >>>Erwin Moller
              >>Thanks, that did the trick. I was just putting my variable before []
              >>instead of after. Odd thing is that when I read it into the database,
              >>I just use $Age[$i], where I thought I would have to use $Age[$i]
              >>$Number. Not sure what is going on under the hood to make $Age[]
              >>$Number into just $Age.
              >>
              >Hi Jerim,
              >>
              >It is just the PHP way of passing arrays around from a form to a
              >receiving script.
              >If PHP receives for example the following 3 name/value pairs (via POST or
              >GET), they are automagically turned into an array:
              >>
              >1) name: Age[] value: 10
              >2) name: Age[] value: 20
              >3) name: Age[] value: 30
              >>
              >If you extract the Age from the posting, like this:
              >$passedAge = $_POST["Age"];
              >PHP 'sees' that Age[] is posted and extracts all the passed values into
              >an array ($passedAge in this case).
              >So if a name ends with the [] PHP knows you mean an array.
              >>
              >It is just the PHP way. :-)
              >>
              >Consider this wrong example:
              >1) name: Age value: 10
              >2) name: Age value: 20
              >3) name: Age value: 30
              >>
              >If you try to extract it now like this:
              >$passedAge = $_POST["Age"];
              >>
              >You'll end up with only the last value (30) and not an array.
              >>
              >So what went wrong in your first try was simply that you didn't end with
              >the
              >[].
              >>
              >Hope that clearifies it a bit.
              >>
              >Good luck & happy coding!
              >>
              >Regards,
              >Erwin Moller
              >
              Just for clarification:
              ending a passed variable with a [] tells PHP it is an array.
              This is NOT a html construct, html doesn't care.
              Right ?
              Right.
              HTML just sends all the form's name/value pairs to whatever is defined as
              'action' in the form.

              Regards,
              Erwin Moller

              >
              bill

              Comment

              • bill

                #8
                Re: Dynamic Radio Buttons

                Erwin Moller wrote:
                bill wrote:
                >
                >Erwin Moller wrote:
                >>Jerim79 wrote:
                >>>
                >>>On Apr 6, 9:08 am, Erwin Moller
                >>><since_human s_read_this_I_a m_spammed_too_m ...@spamyoursel f.comwrote:
                >>>>Jerim79 wrote:
                >>>>>My situation is that I have a form that asks the user for a number.
                >>>>>Next, I execute a while loop that displays a group of questions the
                >>>>>amount of times the customer entered. For instance, the loop looks
                >>>>>this:
                >>>>>while ($Number!=0){
                >>>>> <input type="radio" name="Age[]" value="20-30">20-30
                >>>>> <input type="radio name="Age[]" value="30-40">30-40
                >>>>> <input type="radio name"Age[]" value="40-50">40-50
                >>>>> $Number--;
                >>>>> }
                >>>>>Let's say someone entered 3 for $Number. When the loop executes it
                >>>>>will produce:
                >>>>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
                >>>>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
                >>>>> echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";
                >>>>>The problem is that with radio buttons, you can only choose one
                >>>>>option. With this code, it only allows the user to choose one option
                >>>>>from among the 9 listed, instead of one from each set. I am trying to
                >>>>>find a way to dynamically name each set. If I use checkboxes, then it
                >>>>>records the information correctly and correctly writes it to the
                >>>>>database . However, checkboxes don't look good for this application in
                >>>>>my opinion, and there is no way for me to keep someone from checking
                >>>>>more than one box, that I know of. Here is the PHP code I am using to
                >>>>>catch the user's input and write it to a database:
                >>>>>$Age = $_POST['Age'];
                >>>>>for ($i=0; $i < $Number; $i++) {
                >>>>> $query="INSERT INTO table VALUES('$Age[$i])";
                >>>>>$result = mysql_query($qu ery) or die('Query failed: ' .
                >>>>>mysql_erro r());
                >>>>>}
                >>>>>Like I say, that works fine if I use checkboxes, without changing the
                >>>>>HTML name="Age[]". I am looking for a way to define "sets" of radio
                >>>>>buttons with the same name, so that only one from within a set can be
                >>>>>chosen at a time. Is there something such as:
                >>>>>echo "<group name="1">
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
                >>>>></group>";
                >>>>>echo "<group name=\"2\">
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
                >>>>></group>";
                >>>>>echo "<group name=\"3\">
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
                >>>>> <input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
                >>>>></group>";
                >>>>>This may be more of a HTML question, but I am open to any solution
                >>>>>that would help. Any way to use PHP to accomplish my goal? Even if
                >>>>>there is a way to dynamically name each set such as:
                >>>>>echo "while ($Number!=0){
                >>>>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
                >>>>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
                >>>>> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"40-50\">40-50
                >>>>> $Number--
                >>>>> } ";
                >>>>>I would just have to create a loop to run through each array when
                >>>>>writing to the database, but that shouldn't be a problem.
                >>>>Hi,
                >>>>>
                >>>>You cannot make a group of the radiobuttons since you named them all
                >>>>Age[] and that IS the group as far as HTML is concerned.
                >>>>So just code it in such a way they have different names, like this:
                >>>>>
                >>>><?php
                >>>>// receive number
                >>>>$number = (int)$_POST["number"];
                >>>>?>
                >>>><input type="hidden" name="numberOfA ges" value="<?php echo $number;
                >>>>?>"<?php
                >>>>for ($count=0;$coun t<$number;$coun t++){
                >>>>?>
                >>>> <input type="radio" name="Age<?php echo $number; ?>[]"
                >>>>value="20-30">20-30
                >>>> <input type="radio name="Age[]<?php echo $number; ?>"
                >>>>value="30-40">30-40
                >>>> <input type="radio name"Age[]<?php echo $number; ?>"
                >>>>value="40-50">40-50
                >>>><?php
                >>>> }
                >>>>?>
                >>>>>
                >>>>Now them radiogroups have names like Age0[] and Age1[]
                >>>>>
                >>>>And in the receiving script:
                >>>>$numberOfAg es = $_POST["numberOfAg es"];
                >>>>for ($count=0;$coun t<$numberOfAges ;$count++){
                >>>> $name = "Age".$coun t;
                >>>> $theSelectedVal ue = $_POST[$name];
                >>>> // Do whatever you want with $theSelectedVal ue
                >>>>>
                >>>>}
                >>>>>
                >>>>Not tested, but I hope you get my drift. :-)
                >>>>>
                >>>>Regards,
                >>>>Erwin Moller
                >>>Thanks, that did the trick. I was just putting my variable before []
                >>>instead of after. Odd thing is that when I read it into the database,
                >>>I just use $Age[$i], where I thought I would have to use $Age[$i]
                >>>$Number. Not sure what is going on under the hood to make $Age[]
                >>>$Number into just $Age.
                >>Hi Jerim,
                >>>
                >>It is just the PHP way of passing arrays around from a form to a
                >>receiving script.
                >>If PHP receives for example the following 3 name/value pairs (via POST or
                >>GET), they are automagically turned into an array:
                >>>
                >>1) name: Age[] value: 10
                >>2) name: Age[] value: 20
                >>3) name: Age[] value: 30
                >>>
                >>If you extract the Age from the posting, like this:
                >>$passedAge = $_POST["Age"];
                >>PHP 'sees' that Age[] is posted and extracts all the passed values into
                >>an array ($passedAge in this case).
                >>So if a name ends with the [] PHP knows you mean an array.
                >>>
                >>It is just the PHP way. :-)
                >>>
                >>Consider this wrong example:
                >>1) name: Age value: 10
                >>2) name: Age value: 20
                >>3) name: Age value: 30
                >>>
                >>If you try to extract it now like this:
                >>$passedAge = $_POST["Age"];
                >>>
                >>You'll end up with only the last value (30) and not an array.
                >>>
                >>So what went wrong in your first try was simply that you didn't end with
                >>the
                >>[].
                >>>
                >>Hope that clearifies it a bit.
                >>>
                >>Good luck & happy coding!
                >>>
                >>Regards,
                >>Erwin Moller
                >Just for clarification:
                >ending a passed variable with a [] tells PHP it is an array.
                >This is NOT a html construct, html doesn't care.
                >Right ?
                >
                Right.
                HTML just sends all the form's name/value pairs to whatever is defined as
                'action' in the form.
                >
                Regards,
                Erwin Moller
                >
                >
                >bill
                >
                Thanks, good to know.

                bill

                Comment

                Working...