Home Get HTML Form ID within PHP
Reply: 2

Get HTML Form ID within PHP

J.K. Harthoorn
1#
J.K. Harthoorn Published in 2017-09-14 08:56:52Z

I would like to get the form ID with PHP. This is my HTML code. Each answer within a 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 reive the ID and make it so that

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

How do I do this?

d3r1ck
2#
d3r1ck Reply to 2017-09-14 12:03:19Z

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.

Quentin
3#
Quentin Reply to 2017-09-14 08:59:06Z

You can't. The id is only available client side.

The value is designed for what you are trying to do. Use the value instead.

You'll need to give different answers different values. Your current values do not make sense.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302606 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO