Jump to content

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.