SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to sanitize data but receiving notice - undefined index

    Hello everyone,

    I'm trying to sanitize some data in a form.

    The code in the contact form includes the following:

    HTML Code:
    <form name="form1" method="post" action="form-email.php">
        <input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="50" 
        <input type="submit" name="Submit" />
    </form>
    The code in form-email.php includes:

    PHP Code:
    <?php
        
    if (isset($_POST['Submit'])) {

            if (
    $_POST['name'] != "") {
                
    $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
                if (
    $_POST['name'] == "") {
                    
    $errors .= 'Please enter a valid name.<br/><br/>';
                }
            } else {
                
    $errors .= 'Please enter your name.<br/>';
            }
        }
    ?>
    When I open the page which includes the contact form, I receive a notice:
    Undefined index: name in C:\wamp\www\site1\contact-us.php on line 11


    What does this mean?


    Please help – thank you!
    Last edited by cpradio; Jul 10, 2013 at 05:49. Reason: Added php tags and html tags

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,814
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Since you haven't posted any data yet, the following will throw the Notice you received
    PHP Code:
    <?php echo $_POST['name']; ?>
    To remedy it
    PHP Code:
    <?php echo (isset($_POST['name'])) ? $_POST['name'] : ''?>
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi cpradio,

    that worked!

    Thank you very much for your fast reply.

    Have a good day.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again,

    now I have another problem. When I submit the form I receive a notice:

    Undefined variable: errors in C:\wamp\www\site1\form-email.php on line 11

    The error corresponds to the error message on line11:

    $errors .= 'Please enter your name.<br/>';

    Why would the variable be undefined?


    If someone could help me out. Sorry, but I only have questions...no answers.



    Thank you in advance!

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,814
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Because you never initialized it. Add $errors = array(); above your first if statement and that notice will go away.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi cpradio,

    sorry, where exactly? Like this?


    PHP Code:
    <?php
    $errors 
    = array();
        if (isset(
    $_POST['Submit'])) {

            if (
    $_POST['name'] != "") {
                
    $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
                if (
    $_POST['name'] == "") {
                    
    $errors .= 'Please enter a valid name.<br/><br/>';
                }
            } else {
                
    $errors .= 'Please enter your name.<br/>';
            }
        }
    ?>
    When I submit the form, I now don't receive a notice, but the validation says:

    ArrayPlease enter your name.
    Last edited by cpradio; Jul 10, 2013 at 07:12. Reason: Added php tags

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,814
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Yep, my bad. change it to $errors = '';
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, thanks a lot. Will try it.

    I appreciate your help.

  9. #9
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    it's working now - thank you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •