Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 04-04-2009, 06:42 PM Undefined index
Knight13's Avatar
Defies a Status

Posts: 10,289
Name: Knight13
Location: Cleveland, Ohio
Trades: 0
Ok below is a script that i have just put together to show what is happening

Code:
<?php

        $var = $_POST["test"];
        $len = strlen($var);

        if($len > 0)
        {
                echo $len;        
        }
        else
        {
                echo "Form was not filled in";
        }

?>


# Below is the form.
<form action="today.php" method="POST">
        <input type="text" name="test">
        <input type="submit" value="Submit">
</form>
Now when i go to the page today.php from my wampserver it gives me a notice that said " Notice: Undefined index: test in C:\wamp\www\today.php on line 3 " and i have no idea why it is saying this for, when i type something into the text box it will show the length of the string and the notice will go away but almost every form i make i get that notice on my page.

Does anyone know what is wrong with that and how to fix it because i can not figure it out.
Knight13 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-04-2009, 07:16 PM Re: Undefined index
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
That's because before you send the form, the 'test' field in the $_POST array has not been set, so it doesn't exist. When you submit the form, the variable gets a value, and there is no error any more.

What you'll want to do is check weather or not it has been set, as in
PHP Code:
if (isset($_POST['test'])) {
   
// do things with it
} else {
   
// form have not been submited yet

__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 04-04-2009, 07:19 PM Re: Undefined index
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
This only occurs when the text box is empty correct?
The problem might be with your error reporting. In your php.ini set:

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT

Otherwise try this:
PHP Code:
<?php

        
if(isset($_POST['test']))
        {
                echo 
strlen($_POST['test'];        
        }
        else
        {
                echo 
"Form was not filled in";
        }

?>


# Below is the form.
<form action="today.php" method="POST">
        <input type="text" name="test">
        <input type="submit" value="Submit">
</form>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-04-2009, 11:34 PM Re: Undefined index
Knight13's Avatar
Defies a Status

Posts: 10,289
Name: Knight13
Location: Cleveland, Ohio
Trades: 0
Yes it only happens when the text box is empty the isset() worked, now i am going to try it on all my forms thanks guys.
Knight13 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Undefined index
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.85179 seconds with 12 queries