简体   繁体   English

PHP使用单选按钮的值

[英]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. 因此,我为这些选项创建了2个单选按钮,但我需要使用它们选择显示特定数据的单选按钮的值。 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. 但是我意识到,要使单选按钮的值保存在$ _POST数组中,需要有一个提交按钮。 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 谢谢

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. 我知道,这并不是写某人代码的真正目的,但这可能会给您带来一些启发,使您了解为什么javascript是最好的解决方案,以及为什么PHP无法解决这个问题。 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 PHP是服务器端脚本,不能解决客户端方面的问题,而javascript可以;)您可以尝试使用PHP解决它,这将使其变得非常模棱两可,而javascript非常可悲..: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) (请注意,如果您使用任何类型的javascript框架(例如jquery或prototype),则javascript部分将更容易解决)

You need to add an id to your form tag, so that javascript can find it easily. 您需要在form标签中添加一个ID,以便javascript可以轻松找到它。

You need an "onchange" event on the radio buttons, to submit the form whenever anything changes. 您需要单选按钮上的“ onchange”事件,以便在任何更改时提交表单。

And finally, add a submit button and use javascript to remove it from the page. 最后,添加一个提交按钮,并使用javascript将其从页面中删除。 This way if something goes wrong with the javascript (or if it's turned off), people will still be able to use your website. 这样,如果javascript出现了问题(或已将其关闭),人们仍然可以使用您的网站。 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. 该按钮甚至在屏幕上绘制之前都会被删除,因此对于任何使用JavaScript的人来说,好像都没有该按钮。 Be sure to test the page with and without javascript. 确保使用和不使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM