Reputation: 6126
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
Upvotes: 2
Views: 5907
Reputation: 3463
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)
Upvotes: 1
Reputation: 33379
You need to add an id to your form
tag, so that javascript can find it easily.
You need an "onchange" event on the radio buttons, to submit the form whenever anything changes.
And finally, add a submit button and use javascript to remove it from the page. This way if something goes wrong with the javascript (or if it's turned off), people will still be able to use your website. The button will be deleted before it even gets drawn on the screen, so it's as if it wasn't there for anyone with javascript. Be sure to test the page with and without javascript.
Here's the things you need to add to your code:
<form id="selectnum_form">
<input type="radio" onchange="document.getElementById('selectnum_form').submit()"/>
<input type="radio" onchange="document.getElementById('selectnum_form').submit()"/>
<input type="submit" value="go" id="selectnum_submit"/>
<script type="text/javascript">document.getElementById('selectnum_submit').style.display = 'none';</script>
</form>
Upvotes: 0