在PHP中获取HTML表单ID

在PHP中获取HTML表单ID,php,html,Php,Html,我想用PHP获取表单ID。 这是我的HTML代码。每个问题的每个答案都有一个ID <br><br><h3>Vraag 1: Wat is de juiste formule van Arbeid?</h3> <input type="radio" name="question-1" id="question-1-A" value="2" /> <label for="question-1-A">A) Kra

我想用PHP获取表单ID。 这是我的HTML代码。每个问题的每个答案都有一个ID

<br><br><h3>Vraag 1: Wat is de juiste formule van Arbeid?</h3>

    <input type="radio" name="question-1" id="question-1-A" value="2" />
    <label for="question-1-A">A) Kracht (in Newton) keer Lengte (in meter) </label>

    <input type="radio" name="question-1" id="question-1-B" value="0" />
    <label for="question-1-B">B) Kracht keer Massa</label>

    <input type="radio" name="question-1" id="question-1-C" value="0" />
    <label for="question-1-C">C) Kracht keer valversnelling</label>

    <input type="radio" name="question-1" id="question-1-D" value="0" />
    <label for="question-1-D">D) Geen van deze antwoorden</label>
但是我如何才能收到身份证并使其

if (*QUESTION-1-A* *IS FILLED IN*)
{
     echo "You filled in A"
}

我该怎么做?

你不能。
id
仅在客户端可用

是为您尝试执行的操作而设计的。改用


你需要给出不同的答案,不同的
value
s。您当前的
没有意义。

您不能。
id
仅在客户端可用

是为您尝试执行的操作而设计的。改用


你需要给出不同的答案,不同的
value
s。您当前的
没有意义。

PHP只能访问使用POST/GET传递的值,因此无法访问输入ID或表单ID。 但是,您可以创建一个隐藏元素,如:

<br><br><h3>Vraag 1: Wat is de juiste formule van Arbeid?</h3>

    <input type="radio" name="question-1[]" id="question-1-A" value="2" />
    <label for="question-1-A">A) Kracht (in Newton) keer Lengte (in meter) 
    </label>

    <input type="radio" name="question-1[]" id="question-1-B" value="0" />
    <input type="hidden" name="question-1_ID[]" value="question-1-B"  />
    <label for="question-1-B">B) Kracht keer Massa</label>

    <input type="radio" name="question-1[]" id="question-1-C" value="0" />
    <input type="hidden" name="question-1_ID[]" value="question-1-C"/>
    <label for="question-1-C">C) Kracht keer valversnelling</label>

    <input type="radio" name="question-1[]" id="question-1-D" value="0" />
    <input type="hidden" name="question-1_ID[]" value="question-1-D" />
    <label for="question-1-D">D) Geen van deze antwoorden</label>

希望这有帮助。

PHP只能访问使用POST/GET传递的值,因此无法访问输入ID或表单ID。 但是,您可以创建一个隐藏元素,如:

<br><br><h3>Vraag 1: Wat is de juiste formule van Arbeid?</h3>

    <input type="radio" name="question-1[]" id="question-1-A" value="2" />
    <label for="question-1-A">A) Kracht (in Newton) keer Lengte (in meter) 
    </label>

    <input type="radio" name="question-1[]" id="question-1-B" value="0" />
    <input type="hidden" name="question-1_ID[]" value="question-1-B"  />
    <label for="question-1-B">B) Kracht keer Massa</label>

    <input type="radio" name="question-1[]" id="question-1-C" value="0" />
    <input type="hidden" name="question-1_ID[]" value="question-1-C"/>
    <label for="question-1-C">C) Kracht keer valversnelling</label>

    <input type="radio" name="question-1[]" id="question-1-D" value="0" />
    <input type="hidden" name="question-1_ID[]" value="question-1-D" />
    <label for="question-1-D">D) Geen van deze antwoorden</label>


希望这有帮助。

您可以在
值中传递
id
属性
id
仅在客户端可用,PHP无法工作。以这种方式使用
id
的最佳方式是使用JavaScript而不是PHP。您可以使用jquery获取id,然后通过Ajax将其发送给PHP。您可以在
value
中传递
id
属性
id
仅在客户端可用,PHP无法工作。以这种方式使用
id
的最佳方式是使用JavaScript而不是PHP。您可以使用jquery获取id,然后通过Ajax将其发送给PHP。在这种情况下,他的值可能是为了实现不同的目的:)。@d3r1ck-那么应该使用更合适的方法来实现该目的(我推测这是
选中的
属性)。嗯,它就是这样工作的。我通过将问题的值相加来计算分数。我也可以使值=1A,在PHP中为1A=2,但为什么我会得到向下投票?将评分逻辑移到服务器上。在那里查找每个答案的值范围。限制客户端上的数据,以实际说出用户的答案ving.(如果你把分数放在HTML中,那么人们可以查看源代码以找到正确的答案!)也许他的价值观在这种情况下是为了实现一个不同的目的:)@d3r1ck-那么应该通过更合适的方式来实现这个目的(我想这是
checked
属性).就是这样。我通过将问题的值相加来计算分数。我也可以让value=1A,在php1a=2,但是为什么我会得到一个否决票呢?将评分逻辑移到服务器上。查找每个答案的适用范围。限制客户端上的数据实际说出用户给出的答案。(如果你把分数放在HTML中,那么人们可以查看源代码以找到正确的答案!)无论选择了什么答案,都会得到相同的结果。这些更改没有帮助。只有选中的单选按钮才会成功,因此提交数据中的唯一索引将为
0
。无论选择了什么答案,这都会给出相同的结果。这些更改没有帮助。只有选中的单选按钮才会成功,因此提交数据中的唯一索引将为
0
$questions = ["A","B","C","D"];    
foreach($_POST['question-1'] as $index => $response){
    if($response!==''){
        // then there is something in it.
        echo "You filled in Question ".$questions [$index];
    }
}