单选按钮及其值的 PHP 语法和结构

发布于 2024-08-14 03:07:02 字数 394 浏览 2 评论 0原文

单选框代码:

<input
  type  = "radio"
  name  = "choice"
  value = "A" />Apples />

<input
  type  = "radio"
  name  = "choice"
  value = "B" />Oranges<br />

$choice=array("A"=>1.00, "B"=>0.80);
echo $choice["A"]; // will give me the value of 1.00
echo $choice["B"]; // will give me the value of 0.80

给定上面的代码片段,HTML 单选框、数组或选择是否有问题?

Radio box code:

<input
  type  = "radio"
  name  = "choice"
  value = "A" />Apples />

<input
  type  = "radio"
  name  = "choice"
  value = "B" />Oranges<br />

$choice=array("A"=>1.00, "B"=>0.80);
echo $choice["A"]; // will give me the value of 1.00
echo $choice["B"]; // will give me the value of 0.80

Given the code snippet above, is there anything wrong in terms of either the HTML radio box, the array or the choices?

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

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

发布评论

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

评论(3

森林很绿却致人迷途 2024-08-21 03:07:02

不,看起来很好。
除了苹果文本后面的随机 /> 之外,但我怀疑这是一个拼写错误?

您可能还想考虑这一点。大概你正在走向什么? :)

<?php
$choices = array("A"=>1.00, "B"=>0.80);
if(in_array($_REQUEST['choice'], array_keys($choices))) {
    echo $choices[$_REQUEST['choice']];
}
else {
    echo "Invalid choice received!";
}
?>

这将是接收您的无线电选择的代码。它确保选择有效,然后打印它。

Nope, that looks perfectly fine.
Except for the random /> after your Apples text, but I suspect this is a typo?

You may also want to consider this. Probably what you are heading towards? :)

<?php
$choices = array("A"=>1.00, "B"=>0.80);
if(in_array($_REQUEST['choice'], array_keys($choices))) {
    echo $choices[$_REQUEST['choice']];
}
else {
    echo "Invalid choice received!";
}
?>

This would be the code that receives your radio choice. It makes sure the choice is valid and then prints it.

那支青花 2024-08-21 03:07:02

据我理解你的问题,PHP 代码似乎是错误的。您尚未在 html 代码中指定使用 [ ] 完成的数组。

这就是您在 php 代码中执行的操作,以回显单选的选择:

echo $_REQUEST['choice'];

这将回显选定的单选按钮值 A 或 B。

As far as i understood your question, PHP Code seems to be wrong. You have not specified an array in your html code which is done using [ ].

This is what you do in you php code to echo the selection of the radio:

echo $_REQUEST['choice'];

This will echo selected radio buttons value either A or B.

感性不性感 2024-08-21 03:07:02

我不确定您想要实现什么目标,但有几件事需要您注意。

首先,检查的语法:http: //www.w3schools.com/html/html_forms.asp

所以我将你的html代码更改为:

<form ... >
<input type="radio" name="choice" value="1.00" /> Apples
<br />
<input type="radio" name="choice" value="0.80" /> Oranges
</form> 

在服务器端,实际提交表单后,只需查找 $_POST[ "choice" ] 为您想要的值(1.00 或 0.80)。

I'm not sure what are you trying to achieve, but there are several things that need your attention.

First of all, check the syntax of <input>: http://www.w3schools.com/html/html_forms.asp

So I'd change your html code to:

<form ... >
<input type="radio" name="choice" value="1.00" /> Apples
<br />
<input type="radio" name="choice" value="0.80" /> Oranges
</form> 

And on server side, after actually submitting the form, just look for $_POST[ "choice" ] for your desired value (1.00 or 0.80).

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