Jump to content

I cant figure out whats wrong with my code. Can somebody plz help?


jdock1

Recommended Posts

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

Link to comment
Share on other sites

<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!

 

Link to comment
Share on other sites

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.";
    }
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
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.