Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php contact form error

Options
  • 27-07-2009 1:28am
    #1
    Registered Users Posts: 64 ✭✭


    out of nowhere my contact form is acting up

    It is located here www.freegiftsireland.com/contact.php

    This is what all 3 input boxes are filled with when the page loads
    <font color=ff0000>
    Notice: Undefined index:  message in /usr/local/pem/vhosts/110762/webspace/httpdocs/freegiftsireland/contact_2.php on line 128
    </font>
    

    anyone know what is wrong or how i can solve this


Comments

  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    "Undefined index:" usually refers to an array index that hasn't been set.
    In php, they can look like this...
    $somevar
    $somevar
    $somevar
    If you try to access one that doesn't exist, then you get a warning/notice.

    It's possibe the server was hiding these notices before (you can tell php to not display them), then somebody in charge of the server turned on the notices again, and here we are.

    That page submits to itself... so there should be code in that php file expecting the variables:
    $_POST
    $_POST
    $_POST
    The first time you navagate to this page (ie. you haven't come to contact.php by submitting a form) the $_POST array will be empty... so the above indexes will be undefined... hence the notice.

    What you need to add is some kind of conditional statement that checks if these vars exist using isset() before you try to access them.
    A generally painless way to check if a user is coming from a form submittal is to use something like:
    [php]if(isset($_POST))
    {
    //process form data
    }
    [/php]

    This really is the kind of thing a good contact form should be taking care of anyway.
    If it's throwing up notices over things that're easily solved, I'd be worried about what other measures aren't being taken.
    I'm also a bit weirded-out by where these notices are being displayed... is the script echo'ing straight from the $_POST array into those form elements?! :eek:


  • Registered Users Posts: 6,494 ✭✭✭daymobrew


    Can you post the contact.php code? (inside [ php ] tags)


  • Registered Users Posts: 64 ✭✭noreds


    ok daymobrew i think this is what you were looking for.
    <div class="contact-messages content">
            <?php 
    	  if(isset($_POST['submitted'])) {
    		
    		if($_POST['sp_catcher'] != '') {
    		$spamError = true;
    	}	
    
    		if($_POST['name'] == '') {
    		$nameError = 'You forgot to enter your name.';
    	}
    	
    	if($_POST['email'] == '') {
    		$emailError = 'You forgot to enter your email address';
    	} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $_POST['email'])) {
    		$emailError = 'Enter a valid email address';
    	}
    	
    	if($_POST['message'] == '') {
    		$messageError = 'You forgot to enter the message.';
    	}
    
    	if(!isset($spamError) && !isset($nameError) && !isset($emailError) && !isset($messageError)) {
    		$mailTo = $_POST['mailTo'];
    		$subject = $_POST['subject'];
    		$name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $headers = 'From: '.$name.'<'.$email .'> "\r\n" ' .
    		'X-Mailer: PHP/' . phpversion();
    mail($mailTo, $subject, $message, $headers);
    		?>
            <div class="box success">Thanks ! Your email was successfully sent.</div>
            <?php
    	}else{
    		?>
            <div class="box error">
              <ul class="no-margin">
                <?php if(isset($nameError)) echo '<li class="">'.$nameError.'</li>'; ?>
                <?php if(isset($emailError)) echo '<li class="">'.$emailError.'</li>'; ?>
                <?php if(isset($messageError)) echo '<li class="">'.$messageError.'</li>'; ?>
              </ul>
            </div>
            <?php
    		}
    }else{
    	?>
            <p>Have a question or need some help getting your free gift ! <br />
              We will answer your inquiry as soon as possible.
            <?php
    	}
    ?>
    </p>
                  </div>
    <hr/>
                  <form action="contact.php" method="post" id="sendEmail" class="horizontal">
                    <!-- ================= put your email address here ================ -->
                    <input type="hidden" id="mailTo" name="mailTo" value="offers@freegiftsireland.com" />
                    <!-- ======== you will receive messages with this subject ========= -->
                    <input type="hidden" id="subject" name="subject" value="Free Gift Contact Form" />
                    <!-- ============================================================== -->
                    <input type="hidden" name="submitted" value="1" />
                    <div class="field">
                      <label for="name">Your name</label>
                      <input name="name" id="name" value="<?= $_POST['name']; ?>" type="text"/>
                    </div>
                    <div class="field">
                      <label for="mail">Your email</label>
                      <input name="email" id="mail" value="<?= $_POST['email']; ?>" type="text"/>
                    </div>
                    <div class="field">
                      <label for="message">Your message</label>
                      <textarea name="message" id="message" cols="30" rows="5"><?= $_POST['message']; ?>
    </textarea>
                    </div>
                    <div class="submit-row">
                      <button id="submit" type="submit" class="button" title="Send"><span>Send your message !</span></button>
                      <div id="spam_catcher">
                        <input type="text" value="" name="sp_catcher"/>
                      </div>
                    </div>
                  </form>
    

    as for what DonkeyStyle \o/ I am also worrying about it displaying other stuff.
    I might just totally scrap it and stick up another form


  • Registered Users Posts: 2,793 ✭✭✭oeb


    This is as a result of your error level being set much too high. There is no way that you should be displaying notices on a production server.

    It's basicially down to the fact that no all of the post variables you are using contain information. To prevent the notice use logic blocks to check if they are set before using them (Your specific error is being caused when the page loads ($_POST is empty obviously). You are using the variable where you present the form at the bottom.

    Edit : Also, yes, scrap that form. It is very easy for anyone with any bit of knowhow to use that to send spam (The to email address is contained in a post variable etc)


  • Registered Users Posts: 6,494 ✭✭✭daymobrew


    oeb wrote: »
    Edit : Also, yes, scrap that form. It is very easy for anyone with any bit of knowhow to use that to send spam (The to email address is contained in a post variable etc)
    If the two hidden fields are moved from the form into the php code then it will become more secure.

    For the error messages, you could trap them by changing lines like: [PHP]$message = $_POST;[/PHP] to [PHP]
    $message = (isset($_POST) ? $_POST : '';[/PHP] and use $message instead of $_POST in the form html.


  • Advertisement
Advertisement