如何在PHP中访问标签的值? [英] How to access value of a label in php?

查看:85
本文介绍了如何在PHP中访问标签的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为一个网站创建一个数学问题集,在这个网站上,用户将得到一个等式,并且他们必须在表单中写下答案。

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.

我的HTML标记看起来像这样

My HTML mark up looks like this

    <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

如何使用PHP服务器端脚本执行以下操作:

How do I do the following with PHP server side script:

a)Label的echo值()函数和
b)我如何获得代码将它们存储为变量并输出到屏幕?

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?

PS:我必须在PHP方面做到这一点,我知道这可能适用于Javascript / Jquery。

PS: I have to do it on php side and I am aware this might be do-able with Javascript/Jquery.

谢谢您提前寻求帮助。

Varun

推荐答案

,您无法访问与< label> 有关的任何内容,除了使用会话数据外,最简单的方法是使用< ;输入>

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>.

您需要声明一次值 ,所以您不会得到向用户显示的不同于存储在输入中的值。快速示例:

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" />

这篇关于如何在PHP中访问标签的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆