SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php5 Notice errors in forms

    I have a page in my new website (unpopulated) using forms. This page worked in php4 however the webserver has been upgraded to php5 and my page has gone to pigs & whistles.
    The following snippet of code:
    <form method="post" action="site address(not shown, I haven't got enough stripes ">

    <table border="1">
    <tr><td><img src="images/dot.gif" alt="" />Forename</td><td><input type="text" name="custFirstName" value="<?php echo $custFirstName; ?>" /></td></tr>
    <tr><td><img src="images/dot.gif" alt="" />Surname</td><td><input type="text" name="custLastName" value="<?php echo $custLastName; ?>" /></td></tr>
    <tr><td><img src="images/dot.gif" alt="" />Email Address</td><td><input type="text" name="custEmailAddr" value="<?php echo $custEmailAddr; ?>" /></td></tr>
    <tr><td><img src="images/dot.gif" alt="" />Please confirm<br />email address</td><td><input type="text" name="confEmailAddr" value="<?php echo $confEmailAddr; ?>" /></td></tr>
    </table>
    </form>

    produces the following error messages:
    Notice: Undefined index: custFirstName in \\NAS40ENT\domains\p\petshopkilmarnock.co.uk\user\htdocs\checkOut.php on line 129

    Notice: Undefined index: custLastName in \\NAS40ENT\domains\p\petshopkilmarnock.co.uk\user\htdocs\checkOut.php on line 130

    Notice: Undefined index: custEmailAddr in \\NAS40ENT\domains\p\petshopkilmarnock.co.uk\user\htdocs\checkOut.php on line 131

    Notice: Undefined index: confEmailAddr in \\NAS40ENT\domains\p\petshopkilmarnock.co.uk\user\htdocs\checkOut.php on line 132

    This is only a fragment of the Notice: messages, the page is filled with them (all the same type) and is a real mess!
    Anyone know how to define an index in php5?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The proper solution is to make sure a variable, or an array key(the "index" php is complaining about) exists before trying to use it.

    PHP Code:
    // we didnt check if the foo array key exists in the $_POST variable before trying to use it
    echo $_POST['foo'];

    // check first, then use.
    if (isset($_POST['foo'])) {
        echo 
    $_POST['foo'];

    The band aid way to fix is to tell php not to display notices, by adjusting the error_reporting level.
    PHP Code:
    // top of scripts, or set error_reporting in .htaccess or php.ini
    error_reporting(E_ALL E_NOTICE); 
    although to be honest, any website thats in production mode, and not development mode, shouldnt be showing php errors at all, for both professionalism and security.
    PHP Code:
    ini_set('display_errors'0); 

  3. #3
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi crmalibu, Thanks for replying to my above thread, I have been trying to apply your suggestion of using a pre-defined array to the form input and have decided the problem isn't the form afterall but is being generated by the $_POST statement.
    I have gone to my login page which suffers from the same problem but is smaller and therefore easier to work with.
    The following script:
    Line No
    24 $fstname = stripslashes($_POST['fstname']);
    25 $lstname = stripslashes($_POST['lstname']);
    26 $pword = stripslashes($_POST['pword']);

    if ($lstname=="" or $lstname==" ") { ?>
    <div><span id="administrationPG">&nbsp;
    <form method="post" action="page address(not shown, I haven't got enough stripes/administration.php?">
    <table border="0">
    <tr><td colspan="2">Enter your First & Last Name and Password</td></tr>
    <tr><td>First Name: </td><td><input type="text" name="fstname" value="<?php echo $fstname ?>" size="20"></td></tr>
    <tr><td>Last Name: </td><td><input type="text" name="lstname" value="<?php echo $lstname ?>" size="20"></td></tr>
    <tr><td>Password: </td><td><input type="password" name="pword" value="<?php echo $pword ?>" size="20"></td></tr>
    <tr><td>&nbsp;</td><td><input type="submit" name="submit" value="GO"></td></tr>
    </table>
    </form>
    </span></div>
    <?php } ?>

    produces the following errors:
    Notice: Undefined index: fstname in (page address(not shown, I haven't got enough stripes\Administration.php on line 24)

    Notice: Undefined index: lstname in (page address(not shown, I haven't got enough stripes\Administration.php on line 25)

    Notice: Undefined index: pword in (page address(not shown, I haven't got enough stripes\Administration.php on line 26)

    Its the index in ($_POST['fstname']); which is causing the problem. Substituting an array element produces another error message (Notice
    Many thanks for your interest, Best regards,
    Edward.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats why just put this line on the very much top on the page as crmalibu suggested already then your problem sorted out for that page.
    PHP Code:
    error_reporting(E_ALL & ~E_NOTICE); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It still goes back to that your trying to use a variable or array key indice that doesn't exist. When error_reporting is set to display notices, php "notifies" you that you're using something that doesn't exist. Now, php will by default assign the value of null when you use something that doesn't exist, which is usually ok.

    This is no different from php 4, as php 4 has the exact same behavior and notices. The issue here is the error_reporting level that is configured.

    I gave you two solutions.

  6. #6
    SitePoint Zealot
    Join Date
    May 2006
    Location
    Jhyalipati,Chapagaun-9, Lalitpur, Bagmati, Nepal
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        $custFirstName     = isset($_POST['custFirstName']) ? $_POST['custFirstName'] :"";
        
    $custLastName     = isset($_POST['custLastName']) ? $_POST['custLastName'] :"";
        
    $custEmailAddr     = isset($_POST['custEmailAddr']) ? $_POST['custEmailAddr'] :"";
        
    $confEmailAddr     = isset($_POST['confEmailAddr']) ? $_POST['confEmailAddr'] :""
    Simply Sajan

  7. #7
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi to all who have replied to my original thread, to Raju, Sajan and especially crmalibu who must be a bit frustrated with me? Sorry.
    I concentrated on trying to equate the incoming array with something I was declaring but the error message simply moved to a new position in the script. The (error_reporting(E_ALL & ~E_NOTICE) simply stops the error messages being written on screen and this makes the page look a lot better, however, I have selected to use strict.dtd as the script type in order to force me learn the best XHTML that was possible.
    The recent upgrade of my webhost from php4 to 5 came as a bit of a shock with one page a total mess. When I scan my code with the W3C Validator all of the error messages are visable each generating six or seven errors more in the validator (since the validator appears to report in HTML instead of XHTML which is what we are testing, get rid of the server parser errors (which produce the Notice: reports) and the validator errors drop from over 250 to four or five.
    Thank you Sajan this solution looks most promising, I was thinking that something built into the mysql_fetch_array() was what I needed but unlike the script connecting to a database I didn't have the array name or ID associated with $_POST.
    I will get back to you when I try you script, I am still trying to figure out what the validator is telling me when it drops a table element closing bracket then tells me that the opening and closing tags don't match.
    Many thanks to all.
    Best regards,
    Edward.

  8. #8
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Sajan, many thanks for your post, I have adjusted it for insertion into my login file in place of the original $_POST statements above and it has worked a treat.
    No Notices: reported.
    I now also have my file checkout page validated in w3c validator passed without errors, and will continue with the login page until I get the same result.
    Thanks again.

    Edward.

  9. #9
    SitePoint Zealot
    Join Date
    May 2006
    Location
    Jhyalipati,Chapagaun-9, Lalitpur, Bagmati, Nepal
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its ok, This is the place where we can share our problems, expertise and experiences. Here you can find many gurus.
    Simply Sajan


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
  •