Notice: Undefined index message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stealthmode666
    New Member
    • Nov 2008
    • 21

    Notice: Undefined index message

    New to .php so need script help.
    I want to know how to stop this being displayed when I click submit.

    Notice: Undefined index: homeowner in E:\domains\r\.. .\user\htdocs\. ..\form.php on line 166

    I have an array which takes filled in fields and blank fields and sends them via email.
    I DON'T need the user to fill in all the fields which is resulting in this notice on every field that is not filled in. I do need the fields to be sent tho' even if blank.
    I have posted the scipt i have managed to piece together if this will help.
    somehow I need the blank fields not to show me this notice.
    I have started my .php script with this
    Code:
    <?php 
    $to = $_REQUEST['sendto'] ; 
    $from = $_REQUEST['Email'] ;
    $name = $_REQUEST['Member_Name'] ; 
    $headers = "From: $from"; 
    $subject = "......"; 
    
    $fields = array(); //I HAVE ABOUT 100 FIELDS
    
    $body = ".........:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
    
    $headers2 = "From: .......@....."; 
    $subject2 = "Thank-you ......."; 
    $autoreply = "Somebody.......";
    
    if($from == '') {print "You have not ....";} 
    else { 
    if($name == '') {print "You have not .......";} 
    else { 
    $send = mail($to, $subject, $body, $headers); 
    $send2 = mail($from, $subject2, $autoreply, $headers2); 
    if($send) 
    {header( "Location: http.....com/.../thankyou.html" );} 
    else 
    {print "We encountered an error sending your mail, please notify ...@...."; } 
     }
     }
    ?>
    Sorry, but completey new to .php so I have no idea what i have to do to stop this notice from displaying, or how to script it into the above.
    Any help will be appreciated.
    Last edited by Markus; Nov 28 '08, 10:08 PM. Reason: added # tags
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Stealthmode666,

    For the benefit of our experts and yourself, it is a posting guidelines that all users use [code] tags when posting code. This makes the code easier to read and, in turn, helps our experts answer your questions.

    An easy way to do this is to highlight the code in the textarea and then hit the '#' button at the top of the textarea.

    Please read the Posting Guidlines so you're more familiar with how things work.

    Thanks,
    Moderator.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      You should check with isset() that the array index exists - or you could use ternary operator to set a default value if it doesn't exist.

      Consider the following:

      Code:
      // This will give an index warning because the index doesn't exist.
      $Oh_Crap = $_GET['this_index_doesnt_exist'];
      
      // This helps
      if( isset ($_GET['imaginary_index'] ) )
      {
          // index exists; play with it.
      }
      else
      {
          // index doesn't exist; shout at it!
      }
      
      // or this:
      $This_is_better = ($_GET['imaginary_index'] ? $_GET['imaginary_index'] : "Index doesn't exist");
      Any questions?

      Thanks,
      Markus.

      Comment

      • stealthmode666
        New Member
        • Nov 2008
        • 21

        #4
        Thanks for that, but I have no idea where to insert this?
        I am trying to learn as fast as I can and understand very simple things. I can code in html, css etc but beyond that i'm really struggling, 3 days ago I managed to create a webform, upload it to a webhost, and get the form data to be sent back to me via email and the script above. I am trying to cram in as many tutorials as I can but to be honest a lot of them are incomplete and I need to see them from beginning to end - maybe i'm looking at the wrong websites? sorry for being so dumb but I have to start somewhere.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by stealthmode666
          Thanks for that, but I have no idea where to insert this?
          I am trying to learn as fast as I can and understand very simple things. I can code in html, css etc but beyond that i'm really struggling, 3 days ago I managed to create a webform, upload it to a webhost, and get the form data to be sent back to me via email and the script above. I am trying to cram in as many tutorials as I can but to be honest a lot of them are incomplete and I need to see them from beginning to end - maybe i'm looking at the wrong websites? sorry for being so dumb but I have to start somewhere.
          Ok, the best sites for learning PHP:
          • Tizag.com
          • W3schools.com
          • PHP.net


          Be sure to use them!

          Ok, so what I was saying before was: you need to check the the array index actually has a value. To do that, we can use a few things, but mainly we use isset(). However, I think that'll fatten your code up a bit. Instead, use the ternary operator to assign a default value to your variable if there is no value at the array index. Like follows:

          Code:
          $to = ( isset ($_REQUEST['sendto']) ? $_REQUEST['sendto'] : "default 'to' email goes here" ); 
          $from = ( isset ($_REQUEST['Email']) ? $_REQUEST['sendto'] : "default 'from' email goes here' ) ;
          $name = ( isset ($_REQUEST['Member_Name']) ? $_REQUEST['Member_Name'] : "Default member name goes here' ) ;
          Basically, the warning is saying that the form was not filled out so there are no values in the $_POST, $_GET or $_COOKIE arrays.

          Comment

          • stealthmode666
            New Member
            • Nov 2008
            • 21

            #6
            Not quite there yet.

            Thank-you so much for your help.
            I have used your script but 2 out of 3 have some problems my end.

            I have dragged all my script into dreamweaver to allow me to see the colors which are helping me by showing different tags and types.

            The $to script I have put in place and commented out my old one.
            Sucess, it works great.
            I do not get any notices as long as I fill in the email field.

            Then I introduced the next script which is the $from script.
            This one has caused all code under it including comments to show all in red.

            The $name script has the same effect and problem.
            Do you need to see what i've done and where I've put all this?

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by stealthmode666
              Thank-you so much for your help.
              I have used your script but 2 out of 3 have some problems my end.

              I have dragged all my script into dreamweaver to allow me to see the colors which are helping me by showing different tags and types.

              The $to script I have put in place and commented out my old one.
              Sucess, it works great.
              I do not get any notices as long as I fill in the email field.

              Then I introduced the next script which is the $from script.
              This one has caused all code under it including comments to show all in red.

              The $name script has the same effect and problem.
              Do you need to see what i've done and where I've put all this?
              Ah, that's my fault. Sorry. I've mismatched the quotes, so they aren't closing properly.

              Here's the last 2 lines revised:
              Code:
              $from = ( isset ($_REQUEST['Email']) ? $_REQUEST['sendto'] : "default 'from' email goes here" ) ;
              $name = ( isset ($_REQUEST['Member_Name']) ? $_REQUEST['Member_Name'] : "Default member name goes here" ) ;
              I must've been half asleep when I wrote that...

              My apologies.

              Markus.

              Comment

              • stealthmode666
                New Member
                • Nov 2008
                • 21

                #8
                Thank-you

                I never expected a reply so quickly, so went looking and discovered the single quote instead of double.


                Dreamweaver has helped me with the colours in the sense it caused me to investigate why, so down this long and wonderful road I guess I am starting to learn something.

                You know I will be back. I will mark this thread as solved.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by stealthmode666
                  I never expected a reply so quickly, so went looking and discovered the single quote instead of double.
                  Aha, well you got it! And good job.


                  Originally posted by stealthmode666
                  Dreamweaver has helped me with the colours in the sense it caused me to investigate why, so down this long and wonderful road I guess I am starting to learn something.
                  Syntax highlighting is one of the beautiful luxuries in life.

                  Originally posted by stealthmode666
                  You know I will be back. I will mark this thread as solved.
                  Great stuff.

                  See you around,
                  Markus.

                  Comment

                  • stealthmode666
                    New Member
                    • Nov 2008
                    • 21

                    #10
                    Back again, Still got a page of notices. The last one on the page is the actual field that has not been filled in. I need the user to fill in 2 fields min. then they can hit submit. There are about 100 or so fields and if they don't fill in the 2 required fields or just hit the submit button I'm still getting the page of notices.
                    As long as the 2 fields are filled in then all is fine.
                    i just want it to throw an error to tell them to go back and do what was asked as i need the field information
                    Last edited by stealthmode666; Nov 29 '08, 12:28 PM. Reason: worded better what i want

                    Comment

                    • stealthmode666
                      New Member
                      • Nov 2008
                      • 21

                      #11
                      This is one of two scripts i'm using. Both are the same with the data fields in an array and the first script with about 10 fields on the page works no problem. If the user hits the submit it shows the field that needs to be filled in (I display text to tell them to go back and fill-in the required field) without all the notices appearing about undefined indexes.
                      I don't understand why one works perfectly and the other one shows all the unfilled fields?

                      Comment

                      • stealthmode666
                        New Member
                        • Nov 2008
                        • 21

                        #12
                        Solved the notice:Undefine d index in /....... 128
                        I used .js to insert a space in blank fields left by the user.

                        Comment

                        Working...