Skip to content Skip to sidebar Skip to footer

How To Access Value Of A Label In 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. My HTML mark up looks like this

Solution 1:

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

<p> Please write the sum of these two numbers </p><labelfor="num1">Number1:</label><inputid="num1"name="num1"type="text"value="<?phpecho rand(2,200)?>"readonly="readonly" /><labelfor="num2">Number2:</label><inputid="num2"name="num2"type="text"value="<?phpecho rand(2,200)?>"readonly="readonly" /><inputname="sum"type="text" />

You can use CSS to style those inputs such that they just appear to be more inline text.

Solution 2:

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><labelfor="num1">
    Number1: <?phpecho$num1; ?><inputname="num1"type="hidden"value="<?phpecho$num1; ?>" /></label><labelfor="num2">
    Number2: <?phpecho$num2; ?><inputname="num2"type="hidden"value="<?phpecho$num2; ?>" /></label><inputname="sum"type="text" />

Solution 3:

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.

Solution 4:

Use variables to store the random values and hidden fields to send them with the form.

<formaction="results.php"method="get"><p> Please write the sum of these two numbers </p><?$var1 = rand(2,200);
    $var2 = rand(2,200); ?><inputtype="hidden"name="operand1"value="<?=$var1?>" /><inputtype="hidden"name="operand2"value="<?=$var2?>" /><labelfor="num1">Number1: <?=$var1?></label><labelfor="num2">Number2: <?=$var2?></label><inputname="sum"type="text" /><inputtype="submit"value="submit" /></form>

Yes, I'm using short tags. If you don't, just replace the

Solution 5:

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: <?phpecho$Value1; ?> 
Number2: <?phpecho$Value2; ?><inputtype="hidden"name="Value1"value="<?phpecho$Value1; ?>" /><inputtype="hidden"name="Value2"value="<?phpecho$Value2; ?>" /><inputname="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.

Post a Comment for "How To Access Value Of A Label In Php?"