PHP Using value of radio button

I'm trying to create a registration page where one person can register him/herself along with several other people or just register other people NOT including him/herself. So i've created 2 radio buttons for those options but i need to use the value of the radio button they select to display specific data. This is my code so far

<form name="selectnum" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >
<input type="radio" name="people" value="1"/> I'm attending <br/>
<input type="radio" name="people" value="2"/> I'm not attending <br/>
</form>

<?php if($_POST["people"]==1) { ?>
    How many, counting yourself, are you registering:
    <select name="num_a">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    </select>
<?php } elseif($_POST["people"]==2) { ?>
    How many, NOT counting yourself, are you registering:
    <select name="num_a">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    <option value="19">19</option>
    <option value="20">20</option>
    </select>
<?php } ?>

But I realized that there needs to be a submit button for the values of the radios buttons to get saved in the $_POST array. So instead of using the post data, how do I check to see which radio button the user has selected and display the appropriate question after? Thank you

Answers


It's not really the purpose to write someone's code, I know, but this might give you in extend, some idea why javascript is the best solution, and why PHP can't solve this. PHP is a server-side script, and can't solve problems on the client-side, whereas javascript can ;) You might try solving it with PHP, which will make it very ambiguous and javascript very sad .. :D

Try this code, save your old one, and play with it, it might just be what you like

<script type="text/javascript">
  function changedPeople(radio) {
    switch(radio.value) {
      case '1':
        document.getElementById('people1').style.display = "block";
        document.getElementById('people2').style.display = "none";
        break;
      case '2':
        document.getElementById('people1').style.display = "none";
        document.getElementById('people2').style.display = "block";
        break;

    }
    document.getElementById('submitter').style.display = "block";
  }
</script>

<form name="selectnum" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >
  <input type="radio" name="people" value="1" onchange="javascript:changedPeople(this);"/> I'm attending <br/>
  <input type="radio" name="people" value="2" onchange="javascript:changedPeople(this);"/> I'm not attending <br/>

  <div id="people1" style="display:none;">
    How many, counting yourself, are you registering:
    <select id="people1_select" name="num_a">
    <?php for($i=1;$i<=20;$i++) { ?>
      <option value="<?php echo $i ?>"><?php echo $i ?></option>
    <?php } ?>
    </select>
  </div>

  <div id="people2" style="display:none;">
    How many, NOT counting yourself, are you registering:
    <select id="people1_select" name="num_b">
    <?php for($i=1;$i<=20;$i++) { ?>
      <option value="<?php echo $i ?>"><?php echo $i ?></option>
    <?php } ?>
    </select>
  </div>

  <input type="submit" value="submit" id="submitter" style="display:none;" />
</form>

(Note that the javascript part would have been easier solved if you were using any kind of javascript framework like jquery or prototype)


Need Your Help

What controls should I use for this android UI?

android user-interface components

I'm developing an android application, one page is like this:

How to insert multiple rows from one column?

sql database oracle insert

I want to insert multiple rows from one column by splitting column value. But I have to do that without cursors because of performance issues.

How could I allow only unique strings in a JList?

java swing jlist jcomponent

I have a JList that holds a bunch of strings . I would like it to act like a Set , so that it will only hold unique values . Is there any way to achieve this without checking that a new string does...