Undefined Index Error


I’ve got a contact form which keeps erroring on load up with ‘undefined index’

Any one shed some light on it?



if(!isset($_GET["errors"])) {
	$_SESSION['errors']['txtName'] = 'hidden';
	$_SESSION['errors']['txtEmail'] = 'hidden';
	$_SESSION['errors']['txtContactNumber'] = 'hidden';
	$_SESSION['errors']['txtCompany'] = 'hidden';
	$_SESSION['errors']['txtFailed'] = 'hidden';

	$_SESSION['values']['txtName'] = '';
	$_SESSION['values']['txtEmail'] = '';
	$_SESSION['values']['txtContactNumber'] = '';
	$_SESSION['values']['txtCompany'] = '';

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="keywords" content="" />
<meta name="copyright" content="(c) 2009 RCI Ltd. International " />
<meta name="author" content="Creative Bubbles Web &amp; Graphic Design Agency Nottingham" />
<meta name="description" content="RCI Ltd" />
<title>Ralph Coleman International Ltd - Contact Us </title>
<link href="../Styles/styles.css" rel="stylesheet" type="text/css" />
<script src="../Scripts/scripts.js" type="text/javascript"></script>
<script type="text/javascript" src="../Scripts/validate_other_contact.js"></script>

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAJjF-3JEZeDPMPXpijyXL_hSBQGSDM29aT02LxV4VPFH1xXq9RRS3yf4Wsp1cGFA8iu_dwb9UnaMi8w"
    <script type="text/javascript">
	var map;
    function load() {
      if (GBrowserIsCompatible()) {
	    var geocoder = new GClientGeocoder();

        map = new GMap2(document.getElementById("googleMap"));
		geocoder.getLatLng("Abeles Way, Holly Lane Industrial Estate, Atherstone, Warwickshire CV9 2QX", centerMapCallback);
	function centerMapCallback(returnedPoint) {
		//map.setCenter(returnedPoint, 14);
		var marker = new GMarker(returnedPoint);
		map.setCenter(returnedPoint, 13);
	<script type="text/javascript">
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }

<body onload="javascript:load();">

<div id="maincontainer">

	<!-- Start of Header -->

  <div id="header">
	<div id="logo-quick-launch"><div id="quick-launch">
		<span>For further information call Ralph Coleman on:</span>
        <p id="top-phone-number">01827 716 333</p> 
        <span>or email: <a href="mailto:info@ralphcoleman.net">info@ralphcoleman.net</a></span>
		<div id="logo"><img src="../Images/home/top-bar/logo.jpg" alt="RCI logo" width="101" height="100" /></div>
	<div id="line-table-style">&nbsp;</div>

	<!-- End of Header -->
		<!-- Start of Body -->
  <div id="background">

      	<div id="main-menu">
          <a href="../Transport" target="_top" onclick="MM_nbGroup('down','group1','Transport','',1)" onmouseover="MM_nbGroup('over','Transport','../Images/Common/TransportMenuB.jpg','',1)" onmouseout="MM_nbGroup('out')"><img src="../Images/Common/TransportMenuA.jpg" alt="Transport" name="Transport" width="259" height="45" border="0" id="Transport" onload="" /></a><br />
          <a href="../Warehousing" target="_top" onclick="MM_nbGroup('down','group1','Warehousing','',1)" onmouseover="MM_nbGroup('over','Warehousing','../Images/Common/WarehousingMenuB.jpg','',1)" onmouseout="MM_nbGroup('out')"><img src="../Images/Common/WarehousingMenuA.jpg" alt="Warehousing" name="Warehousing" width="259" height="40" border="0" id="Warehousing" onload="" /></a><br />
          <a href="../TrayWashing" target="_top" onclick="MM_nbGroup('down','group1','TrayWashing','',1)" onmouseover="MM_nbGroup('over','TrayWashing','../Images/Common/TraywashingMenuB.jpg','',1)" onmouseout="MM_nbGroup('out')"><img src="../Images/Common/TraywashingMenuA.jpg" alt="Tray Washing" name="TrayWashing" width="259" height="40" border="0" id="TrayWashing" onload="" /></a><br />
          <a href="../PalletWashing/" target="_top" onclick="MM_nbGroup('down','group1','PalletWashing','',1)" onmouseover="MM_nbGroup('over','PalletWashing','../Images/Common/PalletWashingMenuB.jpg','',1)" onmouseout="MM_nbGroup('out')"><img src="../Images/Common/PalletWashingMenuA.jpg" alt="Pallet Washing" name="PalletWashing" width="259" height="40" border="0" id="PalletWashing" onload="" /></a><br />
          <a href="../Recycling/" target="_top" onclick="MM_nbGroup('down','group1','Recycling','',1)" onmouseover="MM_nbGroup('over','Recycling','../Images/Common/RecyclingMenuB.jpg','',1)" onmouseout="MM_nbGroup('out')"><img src="../Images/Common/RecyclingMenuA.jpg" alt="Recycling" name="Recycling" width="259" height="40" border="0" id="Recycling" onload="" /></a><br />
          <a href="./" target="_top" onclick="MM_nbGroup('down','group1','ContactUs','',1)" onmouseover="MM_nbGroup('over','ContactUs','../Images/Common/ContactMenuB.jpg','',1)" onmouseout="MM_nbGroup('out')"><img src="../Images/Common/ContactMenuB.jpg" alt="Contact Us" name="ContactUs" width="259" height="40" border="0" id="ContactUs" onload="" /></a><br />
        <div id="pageBannerContact">
            <h2>Transport Services</h2>
        <h2>Warehousing Storage</h2>
        <h2>Pallet &amp; Tray Washing</h2>
        <h3>Call  01827 716 333 for further information....</h3>
        <div id="Background">
			<div id="oError">
			<table cellpadding="5" cellspacing="5" >	
				<th><span id="txtFailed" class="<?php echo $_SESSION['errors']['txtFailed'] ?>"><strong>Errors with Enquiry</strong></span></strong></p></th>
		  	<td><span id="txtEmailFailed" class="<?php echo $_SESSION['errors']['txtEmail'] ?>">You've entered a invalid email</span></td>
		  	<td><span id="txtCompanyFailed" class="<?php echo $_SESSION['errors']['txtCompany'] ?>">You haven't entered a company name</span></td>
		  	<td><span id="txtNameFailed" class="<?php echo $_SESSION['errors']['txtName'] ?>">You haven't entered a name</span></td>
		  <td><span id="txtContactNumberFailed" class="<?php echo $_SESSION['errors']['txtContactNumber'] ?>">You need to enter a contact number</span></td>
        	<div id="content">
            	<div id="quick-links"><a href="../">Home</a> > <a href="index.php">Contact Us</a></div>
              <div id="text-content">
              <br />
              <div id="contactForm">
                <form name="frmLogin" action="../Scripts/validate_other_contact.php?validationType=othercontact" method="post" enctype="multipart/form-data">
        <div id="form_tbl_5"><table width="100%" border="0" cellpadding="5" cellspacing="0" class="form_txt">
            <td width="83%" valign="top"><label for="txtName">Name</label>
            <br /><input name="txtName" id="txtName" type="text" class="input" onblur="validate(this.value, this.id)" value="<?php print($_SESSION['values']['txtName']); ?>" /><br /></td>
            <td width="83%" valign="top"><label for="txtCompany">Company</label><br /><input name="txtCompany" id="txtCompany" type="text" class="input" onblur="validate(this.value, this.id)" value="<?php print($_SESSION['values']['txtCompany']); ?>" /></td>
            <td width="83%" valign="top"><label for="txtContactNumber">Phone</label><br /><input name="txtContactNumber" id="txtContactNumber" type="text" class="input" onblur="validate(this.value, this.id)" value="<?php print($_SESSION['values']['txtContactNumber']); ?>" /></td>
            <td width="83%" valign="top"><label for="txtEmail">Email</label><br />
            <input name="txtEmail" id="txtEmail" type="text" class="input" onblur="validate(this.value, this.id)" value="<?php print($_SESSION['values']['txtEmail']); ?>" /></td>
            <td width="83%" valign="top"><label for="txtSubject">Message For</label><br />
            <select id="txtSubject" name="txtSubject">
              <option value="Transport">Transport</option>
              <option value="Warehousing">Warehousing</option>
              <option value="Tray &amp; Pallet Washing">Tray &amp; Pallet Washing</option>
              <option value="Recycling">Recycling</option>
            <td valign="top">What would you like to talk about?<br /><textarea name="enquiry" cols="30" rows="3" class="inputarea" id="enquiry"><?php print($_SESSION['values']['enquiry']); ?></textarea></td>
              <td width="17%"><input name="Reset" type="submit" class="button" value="Send Message" />
                <div id="contactAddress">
                <div id="googleMap"></div>
                 <p>RCI<br />
                  Gresham Road <br />
                  Bermuda Industrial Estate<br />
                  Nuneaton<br />
                  Warwickshire<br />
                  CV10 7QR</p>
                <p><strong>General enquiries:</strong> <br />
                  T: + 44 (0) 2476 324 646<br />
                  F: + 44 (0) 2476 348 242<br />
                  E: <a href="mailto:info@ralphcoleman.net">info@ralphcoleman.net</a> </p>
		<!-- End of Body -->

	  <!-- Start of Sales Boxes -->
      <div id="footerHome2">&nbsp;</div>
       <div id="footer-background">
	<div id="footer-inner-container">
		<div id="footer">
			<div id="left-footer">
			<div class="black-large-font">Further Links</div>
			<p> <a href="../">Home</a></p>
			<p> <a href="../Transport/index.html">Transport</a></p>
            <p> <a href="../Warehousing/index.html">Warehousing</a></p>
			<p> <a href="../TrayWashing/index.php">Tray Washing</a></p>
            <p> <a href="../PalletWashing/index.html">Pallet Washing</a></p>
			<p> <a href="../Recycling/index.html">Recycling</a></p>
			<p> <a href="index.html">Contact us</a></p>
			<div id="right-footer">
			<div class="black-large-font">Contact Details</div>
            <p>Gresham Road</p>
            <p>Bermuda Industrial Estate</p>
            <p>CV10 7QR</p>
			<br />
            <p>Tel: 02476 324 646</p>
			<p>Fax: 02476 348 242</p>



If you corrected the issues here http://ralphcoleman.net/Contact/ there is no more the issue.

That’s why I did not think about putting the check on top of the script or outside the template. But it is obvious and good to have every check and manipulations out side of the template and that implements MVC pattern indeed ! :slight_smile:

oh, for sure, but this is actually for pre-submit, it’s not validation.

Embedding the check within the html code is not necessarily a good idea.

Better might be to use something like this, near the top of the script.

$enquiry = '';
if (isset($_SESSION['values']) && isset($_SESSION['values']['enquiry'])) {
    $enquiry = $_SESSION['values']['enquiry'];

so that you can then use just <?php echo $enquiry ?> within the HTML code.

This is because your PHP error reporting is set to display all errors like notices, warnings and errors and all. So you either can just disable the notices to display or you can better check if the variable is set or not with isset() function. Like in your case, you can do something like this line number 236:

<td valign="top">What would you like to talk about?<br /><textarea name="enquiry" cols="30" rows="3" class="inputarea" id="enquiry"><?php echo isset($_SESSION['values']['enquiry']) ? $_SESSION['values']['enquiry'] : ''; ?></textarea></td>

To avoid of displaying the NOTICEs you can do something like below on top of your script:

error_reporting(E_ALL & ~E_NOTICE);

Yes that’s fine, but when it comes to validating or sanitising variables, I prefer to keep that type of work outside of the template.

It’s pretty standard as far as I am aware, it’s not app logic, it’s similar to if(empty($result)) print paragraph, else make a table with foreach, which is fine in templates.