SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Location
    Tokyo, Japan
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined index and variable errors

    Newbie issue. I'm having some problems with a form that when loaded on an IIS platform running PHP 5.3 throws the following errors:

    PHP Notice: Undefined index: validate
    PHP Notice: Undefined variable: validated
    PHP Notice: Undefined index: name
    PHP Notice: Undefined index: email
    PHP Notice: Undefined index: sub
    PHP Notice: Undefined variable: msg

    I'm fairly new to PHP, but it does boggle my mind that the same form works flawlessly in my MAMP development environment with both PHP 5.2 and 5.3.

    I've posted the code I'm using below, if anyone can provide advice/tips/guidance, I would be very grateful. It's a simple form with some simple validation.

    PHP Code:
    <?php
    if( $_POST['validate'] == 'yes' ) {
            
        if(
    trim($_POST['name']) == '') { // Make sure there is a name
            
    $hasError true;
        }
        else {
            
    $name stripslashes(htmlentities(trim($_POST['name'])));
        }
        
        if(
    trim($_POST['sub']) == '') { // Make sure there is a subject
            
    $hasError true;
        }
        else {
            
    $sub stripslashes(htmlentities(trim($_POST['sub'])));
        }

        if(
    trim($_POST['email']) == '') { // Make sure the email is filled out correctly
            
    $hasError true;
        }
        elseif (!
    preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i"trim($_POST['email']))) {
            
    $hasError true;
        }
        else {
            
    $email trim($_POST['email']);
        }

        if(
    trim($_POST['msg']) == '') { // Make sure there is a msg.
            
    $hasError true;
        }
        else {
                
    $msg stripslashes(htmlentities(trim($_POST['msg'])));
        }
        
        if(isset(
    $_POST['fireinthehole'])) {
            
    $thehole true;
        }

        
    // If the fields not filled correctly, display an error.
        // Else, send the message.
        
    if (isset($hasError)) {
            echo 
    '<p class="error">Please check that all fields contain valid information. Thank you.</p>';
        }
        elseif (isset(
    $thehole)) {
            echo 
    '<p class="error">What are you playing at?</p>';
        }
        else {
            
    $validated true// Validation succeeded. Make sure the form does not display again.
            
    $emailTo 'Put your own email address here'
            
    $body "Name: $name \n\nEmail: $email \n\nSubject: $sub \n\nComments:\n $msg";
            
    $headers 'From: ToyPC Contact Form < '.$emailTo.'>' "\r\n" 'Reply-To: ' $email;
            
            
    mail($emailTo$sub$body$headers);
            
    $emailSent true;
            
    ?>
            <p><strong>Email Successfully Sent!</strong></p>
            <p>Thank you for contacting us. Your message has been sent, and we will be in touch with you shortly.</p>
            <?php 
        
    // end error response
    // end if( $_POST['validate'] == 'yes')
    ?>
    <!-- Begin the form -->
    <?php if(!$validated) { ?>
    <form method="post" action="#form">
        <fieldset>
            <label for="f_name" class="form_label">Name:</label>
            <input type="text" maxlength="255" id="f_name" name="name" value="<?php echo
            
    stripslashes(htmlentities($_POST['name'])); ?>" /><br />
            <label for="f_email" class="form_label">Email:</label>
            <input type="text" maxlength="255" id="f_email" name="email" value="<?php echo
            
    stripslashes(htmlentities($_POST['email'])); ?>" /><br />
            <label for="f_sub" class="form_label">Subject:</label>
            <input type="text" maxlength="255" id="f_sub" name="sub" value="<?php echo
            
    stripslashes(htmlentities($_POST['sub'])); ?>" /><br />
            <label for="f_msg" class="form_label">Message:</label>
            <textarea rows="0" cols="0" id="f_msg" name="msg" ><?php echo
            
    stripslashes(htmlentities($msg)); ?></textarea>
        </fieldset>
        
        <fieldset class="hideme">
            <label for="fireinthehole">Checkbox for spam reduction</label>
            <input type="checkbox" id="fireinthehole" name="fireinthehole" value="1" />
        </fieldset>
        
        <fieldset>
            <input type="hidden" name="validate" value="yes" />
            <input type="submit" value="Submit" class="submit" />
        </fieldset>
    </form>
    <?php /* end if( !$validated ) */ ?>

  2. #2
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the form hasn't been posted, then the keys to the $_POST array won't exist yet. You can get rid of the error by checking with array_key_exists( key, array ) instead of just using $_POST[key]

    Also, you can set $validated = ''; at the top of your script so that it doesn't come back as an undefined variable and just check that $validated != '' lower down.

    Your error reporting settings might be more paranoid on the new setup than the old. As long as this is a development environment, that's a good thing.

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just change
    PHP Code:
    if( $_POST['validate'] == 'yes' ) { 
    to:
    PHP Code:
    if( isset($_POST['validate']) ) { 

  4. #4
    SitePoint Member
    Join Date
    Nov 2009
    Location
    Tokyo, Japan
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Cute Tink and cranial-bore, fixed my issues with your help advice!


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
  •