dongqie4233 2017-09-14 08:56
浏览 46
已采纳

在PHP中获取HTML表单ID

I would like to get the form ID with PHP. This is my HTML code. Each answer per question, has an 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>

So in PHP, I receive the values like this, to calculate the score:

$q1=$_POST['question-1'];

But how can I receive the ID and make it so that

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

How do I do this?

  • 写回答

2条回答 默认 最新

  • dongyu8664 2017-09-14 09:12
    关注

    PHP can only access the values passed using POST/GET so you can't access the input IDs or form IDs. You can however create a hidden element like:

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

    Now when you post the form you can do:

    $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];
        }
    }
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。