php 如何访问php中标签的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7012509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access value of a label in php?
提问by Varun Dave
I am trying to create a math question set for a website where the user will be given an equation and they have to write the answer in the form.
我正在尝试为网站创建一个数学问题集,用户将在其中获得一个方程式,并且他们必须在表格中写下答案。
My HTML mark up looks like this
我的 HTML 标记如下所示
<p> Please write the sum of these two numbers </p>
<label for="num1">Number1: <?php echo rand(2,200)?>
<label for="num2">Number2: <?php echo rand(2,200)?>
<input name="sum" type="text" />
I want to be able to check if the entered the right sum of the two values that are generated. This is just generic at the point. It will eventually end up having more variables and different math operators for the question
我希望能够检查输入的两个生成值的总和是否正确。这在这一点上只是通用的。它最终会为这个问题提供更多的变量和不同的数学运算符
How do I do the following with PHP server side script:
如何使用 PHP 服务器端脚本执行以下操作:
a) The value that the Label's have from echo rand() function and b) how do I get the code to store them as variables and output to the screen?
a) 标签的值来自 echo rand() 函数和 b) 我如何获取代码将它们存储为变量并输出到屏幕?
PS: I have to do it on php side and I am aware this might be do-able with Javascript/Jquery.
PS:我必须在 php 端做,我知道这可能可以用 Javascript/Jquery 来做。
Thank you in advance for your help.
预先感谢您的帮助。
Varun
瓦伦
回答by Endophage
Turn the places you're just echoing the number into readonly text inputs, then the values will get submitted back when the form is submitted (I assume this is part of a larger form as you're not using js).
将您只是回显数字的位置转换为只读文本输入,然后在提交表单时将提交回值(我假设这是较大表单的一部分,因为您没有使用 js)。
<p> Please write the sum of these two numbers </p>
<label for="num1">Number1:</label> <input id="num1" name="num1" type="text" value="<?php echo rand(2,200)?>" readonly="readonly" />
<label for="num2">Number2:</label> <input id="num2" name="num2" type="text" value="<?php echo rand(2,200)?>" readonly="readonly" />
<input name="sum" type="text" />
You can use CSS to style those inputs such that they just appear to be more inline text.
您可以使用 CSS 来设置这些输入的样式,使它们看起来更像是内联文本。
回答by Wesley Murch
Javascript aside, you can't access anything to do with the <label>, the easiest way (aside from using session data) is to use an <input>.
一旁的Javascript,你不能访问任何与该<label>,(除了使用会话数据)的最简单的方法是使用<input>。
You'll want to declare the value once, so you don't get a different value displayed to the user than what is stored in the input. Quick example:
您需要声明一次值,这样您向用户显示的值不会与输入中存储的值不同。快速示例:
<?php
$num1 = rand(2,200);
$num2 = rand(2,200);
?>
<p> Please write the sum of these two numbers </p>
<label for="num1">
Number1: <?php echo $num1; ?>
<input name="num1" type="hidden" value="<?php echo $num1; ?>" />
</label>
<label for="num2">
Number2: <?php echo $num2; ?>
<input name="num2" type="hidden" value="<?php echo $num2; ?>" />
</label>
<input name="sum" type="text" />
回答by Ignas
You can't get the label content just with PHP, you can send it via JS to a php script or save these numbers in hidden fields, but that would mean that a person could change the values of the hidden fields quite easily. The way I would suggest you doing it is to save the random numbers in the user session so that you would have full control over it after the answer is submited or the page is refreshed.
您不能仅使用 PHP 获取标签内容,您可以通过 JS 将其发送到 php 脚本或将这些数字保存在隐藏字段中,但这意味着一个人可以很容易地更改隐藏字段的值。我建议您这样做的方法是在用户会话中保存随机数,以便您在提交答案或刷新页面后可以完全控制它。
回答by Frank Luke
Use variables to store the random values and hidden fields to send them with the form.
使用变量来存储随机值和隐藏字段以将它们与表单一起发送。
<form action="results.php" method="get">
<p> Please write the sum of these two numbers </p>
<? $var1 = rand(2,200);
$var2 = rand(2,200); ?>
<input type="hidden" name="operand1" value="<?= $var1 ?>" />
<input type="hidden" name="operand2" value="<?= $var2 ?>" />
<label for="num1">Number1: <?=$var1?> </label>
<label for="num2">Number2: <?=$var2?> </label>
<input name="sum" type="text" />
<input type="submit" value="submit" />
</form>
Yes, I'm using short tags. If you don't, just replace the
是的,我使用的是短标签。如果你不这样做,只需更换
回答by Daniel Wolfe
You can't. You will need to put the random values into hidden fields:
你不能。您需要将随机值放入隐藏字段:
<p> Please write the sum of these two numbers </p>
<?php
$Value1 = rand(2, 200);
$Value2 = rand(2, 200);
?>
Number1: <?php echo $Value1; ?>
Number2: <?php echo $Value2; ?>
<input type="hidden" name="Value1" value="<?php echo $Value1; ?>" />
<input type="hidden" name="Value2" value="<?php echo $Value2; ?>" />
<input name="sum" type="text" />
Also, I don't think that you want to use label elements here. labels aren't the same in HTML as what you would find in UI systems: in HTML, they have to be attached to a control.
另外,我认为您不想在这里使用标签元素。HTML 中的标签与 UI 系统中的标签不同:在 HTML 中,它们必须附加到控件上。

