jdock1 Posted June 15, 2010 Share Posted June 15, 2010 I fucked with php for a while, then quit messin with it, now im back, reading the manual and plenty of books to try to get my knowledge back up. Im just trying to code this simple access type script real quick from what I can remember. From what I remember, there is nothing wrong with my code. Heres my code; <?php $form = "<center><form action='access.php'> method='get'> <h1>Enter the access code. </h1><input type='text' name='code'> <br><input type='submit'</form></center>"; $code = $_GET['code']; if ($code == "4200"); unset ($form); $form = "Your code was accepted."; ?> <html> <head><title>Access</title></head> <body><?php echo "$form"; ?> </html> And the error I'm getting; Notice: Undefined index: code in C:\wamp\www\access.php on line 7 Im using WAMP on localhost. But it outputs the error, but its still echoing the "unsetted" $form, "Your code was accepted". Could somebody please tell me what I did wrong? Im so frustrated I thought I had this! Haha... Also, if anybody can recommend any step-by-step type PHP book or reference, I'd appreciate it. Thanks Quote Link to comment Share on other sites More sharing options...
DarnMeek Posted June 15, 2010 Share Posted June 15, 2010 <center><form action='access.php'> method='get'> should be <center><form action='access.php' method='get'> and <br><input type='submit'</form></center>"; should be <br><input type='submit'></form></center>"; Remember to put your closing tags appropriately. Don't get frustrated and enjoy yourself! Quote Link to comment Share on other sites More sharing options...
DavidAM Posted June 15, 2010 Share Posted June 15, 2010 I agree with DarnMeek. And also: if ($code == "4200") { unset ($form); $form = "Your code was accepted."; } To get rid of the error message: if (isset($_GET['code'])) { $code = $_GET['code']; if ($code == "4200") { unset ($form); $form = "Your code was accepted."; } } ?> Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted June 15, 2010 Share Posted June 15, 2010 I agree with DarnMeek. And also: if ($code == "4200") { unset ($form); $form = "Your code was accepted."; } To get rid of the error message: if (isset($_GET['code'])) { $code = $_GET['code']; if ($code == "4200") { unset ($form); $form = "Your code was accepted."; } } ?> The usage of unset() does not make any sense. Of course it will work, but it's just not how to do what you're trying to do. And simply using isset() will still return true even if there is no value, so you still get the Undefined variable error. Use empty to check for a NULL value. if (!empty($_GET['code'])) { $code = $_GET['code']; if ($code == "4200") { unset ($form); $form = "Your code was accepted."; } } else { echo 'Please enter a code'; } Always wise to check for data type as well. If $_GET['code'] is always to be numeric, test against ctype_digit. Quote Link to comment Share on other sites More sharing options...
jdock1 Posted June 15, 2010 Author Share Posted June 15, 2010 Wow, thank you all so much. Okay, well now I discovered a new problem. It must be how my server is configured. Everytime I use $_GET, I get the same error message , Notice: Undefined index: sample for example, I create a variable, $sample. I make a form with a text field named sample that will be the variable, like so $sample = "$_GET['sample']; ive done this on every page and I still get the same error message. What could this be? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted June 15, 2010 Share Posted June 15, 2010 are you inputting anything into the text field "sample"? Quote Link to comment Share on other sites More sharing options...
jdock1 Posted June 17, 2010 Author Share Posted June 17, 2010 are you inputting anything into the text field "sample"? Well yes of course, I get the error once the page first loads & the text field is empty. Once I input parameters, the error goes away. I always have to put the @ before the variable so it don't output an error. So it must be something in the configuration of my PHP? Anybody know how to fix this? Its so fucking annoying. Quote Link to comment Share on other sites More sharing options...
firedealer Posted June 17, 2010 Share Posted June 17, 2010 Try to add this one? $_GET['sample'] = empty($_GET['sample']) ? NULL : $_GET['sample']; Quote Link to comment Share on other sites More sharing options...
joel24 Posted June 17, 2010 Share Posted June 17, 2010 try print_r($_GET); and see if it lists any $_GET values. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 17, 2010 Share Posted June 17, 2010 A "notice" is just that -- it's something that you may or may not need to be aware of. It's not an error. That particular notice is telling you that you are trying to reference an array location in the $_GET variable that doesn't exist. the $_GET[] array contains array elements for an url parameters that were passed along when the script was run. Since you're trying to implement a self-posting form using the GET method, this script will always produce that notice when the form is emitted. If you want to avoid that, you can check isset($_GET['code']) prior to attempting to set a variable to it. Quote Link to comment 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.