当您从 html 表单中选择单选按钮时,我可以指定您希望运行的 php 代码块吗?

发布于 2024-12-11 16:21:25 字数 1873 浏览 0 评论 0原文

例如,我有许多用户可以填写的字段,但我希望 PHP 代码根据他们选择的单选按钮输出不同的内容。

 <html>
<body>

<form action="citation.php" method="post">
<table width="100%" border="0">
  <tr>
    <td>Name</td>
    <td><label for="name2"></label>
      <input type="text" name="name" id="name2"></td>
  </tr>
  <tr>
    <td>Number</td>
    <td><label for="number"></label>
      <input type="text" name="number" id="number"></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><label for="email"></label>
      <input type="text" name="email" id="email"></td>
  </tr>
  <tr>
    <td>Address</td>
    <td><label for="address"></label>
      <input type="text" name="address" id="address"></td>
  </tr>
  <tr>
    <td>web</td>
    <td><label for="web"></label>
      <input type="text" name="web" id="web"></td>
  </tr>
  <tr>
    <td><p>
      <label>
          <input type="radio" name="RadioGroup1" value="ex1" id="RadioGroup1_0">
          ex1</label>
        <br>
        <label>
          <input type="radio" name="RadioGroup1" value="ex2" id="RadioGroup1_1">
          ex2</label>
        <br>
        <label>
          <input type="radio" name="RadioGroup1" value="ex3" id="RadioGroup1_2">
          ex3</label>
        <br>
    </p></td>
    <td><input type="submit" name="click me" id="click me" value="Submit"></td>
  </tr>
</table>
<p>&nbsp;</p>
</form>
</body>
</html>

因此,如果他们选择第一个单选按钮并单击“提交”,则会输出与选择第二个单选按钮不同的内容。

For example, I have a number of fields a user could fill out but I want the PHP code to spit out something different depending on which radio button they select.

 <html>
<body>

<form action="citation.php" method="post">
<table width="100%" border="0">
  <tr>
    <td>Name</td>
    <td><label for="name2"></label>
      <input type="text" name="name" id="name2"></td>
  </tr>
  <tr>
    <td>Number</td>
    <td><label for="number"></label>
      <input type="text" name="number" id="number"></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><label for="email"></label>
      <input type="text" name="email" id="email"></td>
  </tr>
  <tr>
    <td>Address</td>
    <td><label for="address"></label>
      <input type="text" name="address" id="address"></td>
  </tr>
  <tr>
    <td>web</td>
    <td><label for="web"></label>
      <input type="text" name="web" id="web"></td>
  </tr>
  <tr>
    <td><p>
      <label>
          <input type="radio" name="RadioGroup1" value="ex1" id="RadioGroup1_0">
          ex1</label>
        <br>
        <label>
          <input type="radio" name="RadioGroup1" value="ex2" id="RadioGroup1_1">
          ex2</label>
        <br>
        <label>
          <input type="radio" name="RadioGroup1" value="ex3" id="RadioGroup1_2">
          ex3</label>
        <br>
    </p></td>
    <td><input type="submit" name="click me" id="click me" value="Submit"></td>
  </tr>
</table>
<p> </p>
</form>
</body>
</html>

So, if they choose the first radio button and hit submit, that would spit out something different than if you chose the 2nd radio button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素食主义者 2024-12-18 16:21:25

这是非常基本的 PHP

<?php
  if($_POST['RadioGroup1']=='ex1'){
    ?>
     You hit the first radio button, now he's hurt!
    <?
  }elseif($_POST['RadioGroup1']=='ex2'){
     ?>
     You hurted the feelings of the second radio btn
     <?
  }elseif($_POST['RadioGroup1']=='ex3'){
    ?>
     The tird radio button was clicked by you
    <?
  }
?>

甚至更好:

<?php
  switch($_POST['RadioGroup1'){
     case 'ext1':
        ?>
         You hit the first radio button, now he's hurt!
       <? 
      break;
     case 'ext2':
        ?>
          You hurted the feelings of the second radio btn
       <? 
      break;
     case 'ext3':
        ?>
          The tird radio button was clicked by you
       <? 
      break;
        default: die('wrong')
    }
  }
?>

您还可以包含文件,而不是将 HTML 粘贴到 PHP 中:

<?php
  switch($_POST['RadioGroup1'){
     case 'ext1':
        include('form/1clicked.php')
      break;
      ....
    }
  }
?>

it's quite basic PHP

<?php
  if($_POST['RadioGroup1']=='ex1'){
    ?>
     You hit the first radio button, now he's hurt!
    <?
  }elseif($_POST['RadioGroup1']=='ex2'){
     ?>
     You hurted the feelings of the second radio btn
     <?
  }elseif($_POST['RadioGroup1']=='ex3'){
    ?>
     The tird radio button was clicked by you
    <?
  }
?>

or even nicer:

<?php
  switch($_POST['RadioGroup1'){
     case 'ext1':
        ?>
         You hit the first radio button, now he's hurt!
       <? 
      break;
     case 'ext2':
        ?>
          You hurted the feelings of the second radio btn
       <? 
      break;
     case 'ext3':
        ?>
          The tird radio button was clicked by you
       <? 
      break;
        default: die('wrong')
    }
  }
?>

instead of pasteing HTML in th the PHP, you could also include files:

<?php
  switch($_POST['RadioGroup1'){
     case 'ext1':
        include('form/1clicked.php')
      break;
      ....
    }
  }
?>
逐鹿 2024-12-18 16:21:25

当您进行标准表单提交时,表单字段的 name 字段会自动作为 POST 参数传递给 php,即在 $_POST 中。如果字段具有 name="test"value="valueTest",那么在 php 层,您将获得该值 $_POST['test ']。以下是如何处理您的特定情况的示例:

    <?php

        switch ($_POST['RadioGroup1']) {
            case 'ex1':
                // handle ex1 case
                break;
            case 'ex2':
                // handle ex2 case
                break;
            case 'ex3':
                // handle ex3 case
                break;
            default:
                // handle cases here where RadioGroup1 has invalid value
                break;
        }

    ?>

When you do standard form submission, the name field of form fields automatically are passed to php as POST params, i.e., in $_POST. If a field has name="test" and value="valueTest", then at the php layer, you will get that value as $_POST['test']. Below is an example of how to handle your particular case:

    <?php

        switch ($_POST['RadioGroup1']) {
            case 'ex1':
                // handle ex1 case
                break;
            case 'ex2':
                // handle ex2 case
                break;
            case 'ex3':
                // handle ex3 case
                break;
            default:
                // handle cases here where RadioGroup1 has invalid value
                break;
        }

    ?>
成熟的代价 2024-12-18 16:21:25
if(isset($_POST['firstradio']))
{
echo "Content for First Radio";
}
elseif(isset($_POST['secondradio']))
{
echo "Content for Second Radio";
}
else
{
echo "Something";
}
if(isset($_POST['firstradio']))
{
echo "Content for First Radio";
}
elseif(isset($_POST['secondradio']))
{
echo "Content for Second Radio";
}
else
{
echo "Something";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文