Jump to content

Undefined Variable


dean7

Recommended Posts

Hey, I'm trying to code a code a user has to input before continuing the registering to my site, for example what I'm trying to do is this: 1 + 3 = 5. So it will randomly give 2 numbers which then the user has to submit the right answer to carry on although its saying I have a undefined variable, which the variable I'm using is defined.

 

My code:

if ($Code){$NumberOne = 1;$NumberTwo = 50;$RandomNum = rand($NumberOne, $NumberTwo);$RandomNum1 = rand($NumberTwo, $NumberOne);$Output = $RandomNum;$Output1 = $RandomNum1; // Doing the sum..if ($Output >= 1 || $Output1 >= 1){$Sumfin = $Output + $Output1;$Eq = $SumFin; if ($Sumfin != $Eq){$Message = "Incorrect number adding";$Succ = "no";}elseif ($Sumfin == $Eq){$Succ = "yes";} }elseif ($Output > 50 || $Output1 > 50){$Message = "Something went wrong.";$Succ = "no";}}

 
How I'm out putting it:
<td width="50%">Secruity Code:</td><td width="50%"><?php echo $Output;?> + <?php echo $Output1; ?>: <input type="text" name="Code" id="Scode" class="TextInput" maxlength="3"></td>
 
So what can I do here differently to remove the error?
 
Thanks for your help :)
 
Link to comment
https://forums.phpfreaks.com/topic/283929-undefined-variable/
Share on other sites

Just posting snippets won't help. The only reason that we can assume from that is $Code is not set and you are echoing it out of that conditional.

There the only errors its getting, but with $Code I'm doing: 

 

if (strip_tags($_POST['Code'])){
 
$Code = safe($_POST['Code']);
 
// Then that code.
}

Link to comment
https://forums.phpfreaks.com/topic/283929-undefined-variable/#findComment-1458398
Share on other sites

Maybe you should rethink your logic.

 

The variables $Output and $Output1 are only defined when the variable $_POST['Code'] exists, In other words when the form has been submitted. Obviously you want these variables defined when the form has not not been submitted so the user can give an answer to your sum.

 

However that is only one part of the problem. The next issue you'll face is how are you going to know the user gave the correct answer to the sum? The variables $Output and $Output1 will not be remembered. You'll need to pass on the answer to the sum (either as a hidden form field or as a session/cookie value), so when form has been submitted your code can validate that the user gave the correct answer.

Link to comment
https://forums.phpfreaks.com/topic/283929-undefined-variable/#findComment-1458409
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.