php单选框值

发布于 2024-08-14 05:19:29 字数 562 浏览 2 评论 0原文

鉴于使用 html 单选框时,要将值发送到 php 脚本,只有选定的值会发送到 php 脚本,仍然成立。

当我只对发送到 php 脚本的所选值感兴趣时,我是否还需要检查用户是否选择了某些内容,如果用户没有选择任何内容,我们什么都不做,或者我想我可以提示用户选择某些内容。

如果是这样,处理无线电盒的最佳方法是什么?

 <!--Using the radio box, a single selected value is sent to the php script for processing-->
    <td width="100px" height="30px">  
    <input type = "radio"
    name = "selection"
    value = "Lays" />Lays Chips 0.99c<br />

    <input type = "radio"
    name = "selection"
    value = "Ruffles" />Ruffles $1.85<br />

Given that when using an html radio box, to send values to a php script, only the selected values is sent to the php script, still holds true.

Do I still need to check that the user has selected something, when I am only interested in the selected value being sent to the php script, if the user does not select anything we do nothing or I guess I could prompt the user to select something.

If so, whats the best way to handle this with regards to radio boxes?

 <!--Using the radio box, a single selected value is sent to the php script for processing-->
    <td width="100px" height="30px">  
    <input type = "radio"
    name = "selection"
    value = "Lays" />Lays Chips 0.99c<br />

    <input type = "radio"
    name = "selection"
    value = "Ruffles" />Ruffles $1.85<br />

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

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

发布评论

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

评论(2

给不了的爱 2024-08-21 05:19:29

即使不从单选框中选择选项,用户也可以单击“提交”按钮。

如果可以在没有选择的情况下发送表单,并且您只想在未选择任何内容时执行不同的操作(或忽略它),则可以执行以下操作:

<?php
  if (isset(($_POST['selection'])) {
    //Use it.. and make sure to validate user input.
  } else {
    //nothing was selected. Do something or just ignore?
  }
?>

OTOH,如果您想阻止在没有选择的情况下提交,则需要使用一些 JavaScript 来完成它。

The user will be able to click the "submit" button even without selecting an option from your radiobox.

If it's ok to send the form without a selection and you just want to do something different (or ignore it) when nothing is selected, you can do this:

<?php
  if (isset(($_POST['selection'])) {
    //Use it.. and make sure to validate user input.
  } else {
    //nothing was selected. Do something or just ignore?
  }
?>

OTOH, if you want to prevent submiting without a selection, you will need to use some JavaScript to do it.

蘑菇王子 2024-08-21 05:19:29

即使未设置radiobox,您仍然可以将表单发送回服务器。当然,除非您的 Javascript 在未设置 radiobox 的情况下阻止用户点击提交表单。

因此,在进行操作之前,您仍然需要检查 radiobox 是否已设置。

Even if the radiobox is not set, you can still post the form back to the server. Unless, of course, you have Javascript that prevents one from clicking on the submit form if the radiobox is not set.

So, you still need to check whether the radiobox is set or not, before working on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文