By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

Undefined index

P: 2
Hello I have a php contact email form on my a ecommerce shop website !

I am getting a Undefined index error messages coming from the top of the php code, the email form its self works fine,

HERE IS THE ERROR MESSAGES


Notice: Undefined index: Name in E:\domains\t\tools2diy.co.uk\user\htdocs\contact.p hp on line 13

Notice: Undefined index: Tel in E:\domains\t\tools2diy.co.uk\user\htdocs\contact.p hp on line 14

Notice: Undefined index: Email in E:\domains\t\tools2diy.co.uk\user\htdocs\contact.p hp on line 15

Notice: Undefined index: Message in E:\domains\t\tools2diy.co.uk\user\htdocs\contact.p hp on line 16



The code im using is as follows :


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_cache_limiter('none');
  3. session_start();
  4. ob_start();
  5. ?>
  6.  
  7. <?php
  8.  
  9. $EmailFrom = "info@mediadiverse.org.uk";
  10. $EmailTo = "info@mediadiverse.org.uk";
  11. $Subject = "Tools2diy Enquiry";
  12. $Name = Trim(stripslashes($_POST['Name'])); 
  13. $Tel = Trim(stripslashes($_POST['Tel'])); 
  14. $Email = Trim(stripslashes($_POST['Email'])); 
  15. $Message = Trim(stripslashes($_POST['Message'])); 
  16.  
  17. // validation
  18. if (isset($_POST['submit'])) $validate = validateform($Name, $Tel, $Email, $Message);
  19.  
  20. // prepare email body text
  21. $Body = "";
  22. $Body .= "Name: ";
  23. $Body .= $Name;
  24. $Body .= "\n";
  25. $Body .= "Tel: ";
  26. $Body .= $Tel;
  27. $Body .= "\n";
  28. $Body .= "Email: ";
  29. $Body .= $Email;
  30. $Body .= "\n";
  31. $Body .= "Message: ";
  32. $Body .= $Message;
  33. $Body .= "\n";
  34.  
  35. // send email 
  36. if (isset($_POST['submit']) && $validate['false'] != true){
  37.  mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
  38.  $emailsent = true;
  39. }
  40.  
  41. function validateform($Name, $Tel, $Email, $Message){
  42.   //Check empty fields
  43.   if ($Name == ''){
  44.     $return['false'] = true;
  45.     $return['nameblank'] = true;
  46.   }
  47.   if ($Tel == ''){
  48.     $return['false'] = true;
  49.     $return['telblank'] = true;
  50.   }
  51.   if ($Email == ''){
  52.     $return['false'] = true;
  53.     $return['emailblank'] = true;
  54.   }
  55.  
  56.   if ($Message == ''){
  57.     $return['false'] = true;
  58.     $return['messageblank'] = true;
  59.   }
  60.  
  61.   //Check phone number is digits and spaces only
  62.   if (!is_numeric(str_replace(' ','',$Tel))){
  63.    $return['phone'] = true;
  64.    $return['false'] = true;
  65.   }
  66.  
  67.   //Check email is valid
  68.   if (!preg_match('/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/',$Email)){
  69.    $return['email'] = true;
  70.    $return['false'] = true;
  71.   }
  72.  
  73.   //CAPTCHA
  74.   if($_POST['secure'] != $_SESSION['security_number']){
  75.    $return['captcha'] = true;
  76.    $return['false'] = true;
  77.   }
  78.  
  79.   return $return;
  80.  
  81. }
  82. ?>


The strange thing is that im using the exact same code in another site which is hosted on another server ! but its error free and my current hosting have told me its not the hosting so im stuck !

Any help would be great
Jun 7 '10 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
it just means, those indices are not in the $_POST array, either because there are no $_POST data at all (e.g. no form submitted via POST) or the indices are spelled wrong.
test with var_dump($_POST);
Jun 7 '10 #2

P: 2
Hi the form is below ! the form works just cant get rid of the errors




PHP ABOVE HEADER

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. $EmailFrom = "info@mediadiverse.org.uk";
  4. $EmailTo = "info@mediadiverse.org.uk";
  5. $Subject = "Tools2diy Enquiry";
  6. $Name = Trim(stripslashes($_POST['Name'])); 
  7. $Tel = Trim(stripslashes($_POST['Tel'])); 
  8. $Email = Trim(stripslashes($_POST['Email'])); 
  9. $Message = Trim(stripslashes($_POST['Message'])); 
  10.  
  11. // validation
  12. if (isset($_POST['submit'])) $validate = validateform($Name, $Tel, $Email, $Message);
  13.  
  14. // prepare email body text
  15. $Body = "";
  16. $Body .= "Name: ";
  17. $Body .= $Name;
  18. $Body .= "\n";
  19. $Body .= "Tel: ";
  20. $Body .= $Tel;
  21. $Body .= "\n";
  22. $Body .= "Email: ";
  23. $Body .= $Email;
  24. $Body .= "\n";
  25. $Body .= "Message: ";
  26. $Body .= $Message;
  27. $Body .= "\n";
  28.  
  29. // send email 
  30. if (isset($_POST['submit']) && $validate['false'] != true){
  31.  mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
  32.  $emailsent = true;
  33. }
  34.  
  35. function validateform($Name, $Tel, $Email, $Message){
  36.   //Check empty fields
  37.   if ($Name == ''){
  38.     $return['false'] = true;
  39.     $return['nameblank'] = true;
  40.   }
  41.   if ($Tel == ''){
  42.     $return['false'] = true;
  43.     $return['telblank'] = true;
  44.   }
  45.   if ($Email == ''){
  46.     $return['false'] = true;
  47.     $return['emailblank'] = true;
  48.   }
  49.  
  50.   if ($Message == ''){
  51.     $return['false'] = true;
  52.     $return['messageblank'] = true;
  53.   }
  54.  
  55.   //Check phone number is digits and spaces only
  56.   if (!is_numeric(str_replace(' ','',$Tel))){
  57.    $return['phone'] = true;
  58.    $return['false'] = true;
  59.   }
  60.  
  61.   //Check email is valid
  62.   if (!preg_match('/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/',$Email)){
  63.    $return['email'] = true;
  64.    $return['false'] = true;
  65.   }
  66.  
  67.   //CAPTCHA
  68.   if($_POST['secure'] != $_SESSION['security_number']){
  69.    $return['captcha'] = true;
  70.    $return['false'] = true;
  71.   }
  72.  
  73.   return $return;
  74.  
  75. }
  76. ?>



BODY CONTENT FORM

Expand|Select|Wrap|Line Numbers
  1. <?php if (isset($emailsent) && ($emailsent == true))
  2.         echo '<p style="text-align: center">Your message has been sent we will be in touch soon!</p>';
  3. ?>
  4.  
  5. <div id="contact-area">
  6.  
  7.                 <form method="post" action="contact.php">
  8.  
  9.                 <div><label for="Name" id="Name">Your name:</label>
  10.                 <input type="text" name="Name" value="<?php if (isset($_POST['Name'])) echo htmlentities($_POST['Name']) ?>"/>
  11.                 <?php if (isset($validate['nameblank'])) echo '<span class="formerror">You need to complete this field</span>'; ?>
  12.                 </div>
  13.  
  14.                 <div><label for="Tel" id="Tel">Your Tel:</label>
  15.                 <input type="text" name="Tel" value="<?php if (isset($_POST['Tel'])) echo htmlentities($_POST['Tel']) ?>"/>
  16.                 <?php if (isset($validate['telblank'])) echo '<span class="formerror">You need to complete this field</span>'; ?>
  17.                     <?php if (isset($validate['phone'])) echo '<span class="formerror">Invalid phone number</span>'; ?>
  18.                   </div>
  19.  
  20.                     <div><label for="Email" id="Email">Your email:</label>
  21.                 <input type="text" name="Email" value="<?php if (isset($_POST['Email'])) echo htmlentities($_POST['Email'])?>"/>
  22.                 <?php if (isset($validate['emailblank'])) echo '<span class="formerror">You need to complete this field</span>'; ?>
  23.                     <?php if (isset($validate['email'])) echo '<span class="formerror">Invalid email address</span>'; ?>
  24.                 </div>
  25.  
  26.                 <div><label for="Message" id="Message">Your message :</label>
  27.                 <textarea name="Message" rows="20" cols="20"><?php if (isset($_POST['Message'])) echo htmlentities($_POST['Message']) ?></textarea>
  28.                 <?php if (isset($validate['messageblank'])) echo '<span class="formerror">You need to complete this field</span>'; ?>
  29.                 </div>
  30.  
  31.                  <div>
  32.                <label for="captcha" id="captcha">Please answer this </label>
  33.                 <img src="includes/image.php" alt="CAPTCHA" name="captcha" id="captcha"  />
  34.  
  35. </div>
  36.  
  37.  
  38.                 <div><label>Input the answer</label>
  39.                  <input type="text" rows="20" cols="20" name="secure" />
  40.                 <?php if (isset($validate['captcha'])) echo '<span class="formerror">Invalid response</span>'; ?>
  41.                 </div>
  42.  
  43.  
  44.  
  45.                 <div><label><input type="submit" name="submit" value="Submit" class="submit-button" />
  46.                 </label>
  47.                 </div>
  48.             </form>
Jun 7 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
kill the email script, if you don’t find the $_POST variables.
Jun 7 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.