dean7 Posted November 15, 2013 Share Posted November 15, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/283929-undefined-variable/ Share on other sites More sharing options...
Ch0cu3r Posted November 15, 2013 Share Posted November 15, 2013 What undefined variable are you getting? Post the error message(s) in full here. Quote Link to comment https://forums.phpfreaks.com/topic/283929-undefined-variable/#findComment-1458395 Share on other sites More sharing options...
dean7 Posted November 15, 2013 Author Share Posted November 15, 2013 Notice: Undefined variable: Output in C:\wamp\www\register.php on line 155 Notice: Undefined variable: Output1 in C:\wamp\www\register.php on line 155 Quote Link to comment https://forums.phpfreaks.com/topic/283929-undefined-variable/#findComment-1458396 Share on other sites More sharing options...
MDCode Posted November 15, 2013 Share Posted November 15, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283929-undefined-variable/#findComment-1458397 Share on other sites More sharing options...
dean7 Posted November 15, 2013 Author Share Posted November 15, 2013 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. } Quote Link to comment https://forums.phpfreaks.com/topic/283929-undefined-variable/#findComment-1458398 Share on other sites More sharing options...
Ch0cu3r Posted November 15, 2013 Share Posted November 15, 2013 (edited) 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 November 15, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283929-undefined-variable/#findComment-1458409 Share on other sites More sharing options...
dean7 Posted November 15, 2013 Author Share Posted November 15, 2013 Ok, right I'll give it ago with sessions Quote Link to comment https://forums.phpfreaks.com/topic/283929-undefined-variable/#findComment-1458413 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.