Reputation: 179
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
<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
How do I do the following with PHP server side script:
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: I have to do it on php side and I am aware this might be do-able with Javascript/Jquery.
Thank you in advance for your help.
Varun
Upvotes: 0
Views: 33851
Reputation: 662
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: <?php echo $Value1; ?>
Number2: <?php echo $Value2; ?>
<input type="hidden" name="Value1" value="<?php echo $Value1; ?>" />
<input type="hidden" name="Value2" value="<?php echo $Value2; ?>" />
<input name="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.
Upvotes: 0
Reputation: 102745
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" />
Upvotes: 1
Reputation: 1400
Use variables to store the random values and hidden fields to send them with the form.
<form action="results.php" method="get">
<p> Please write the sum of these two numbers </p>
<? $var1 = rand(2,200);
$var2 = rand(2,200); ?>
<input type="hidden" name="operand1" value="<?= $var1 ?>" />
<input type="hidden" name="operand2" value="<?= $var2 ?>" />
<label for="num1">Number1: <?=$var1?> </label>
<label for="num2">Number2: <?=$var2?> </label>
<input name="sum" type="text" />
<input type="submit" value="submit" />
</form>
Yes, I'm using short tags. If you don't, just replace the
Upvotes: 0
Reputation: 1965
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.
Upvotes: 0
Reputation: 21473
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>
<label for="num1">Number1:</label> <input id="num1" name="num1" type="text" value="<?php echo rand(2,200)?>" readonly="readonly" />
<label for="num2">Number2:</label> <input id="num2" name="num2" type="text" value="<?php echo rand(2,200)?>" readonly="readonly" />
<input name="sum" type="text" />
You can use CSS to style those inputs such that they just appear to be more inline text.
Upvotes: 3