0

I have a contact form on a .php coded website that runs off a cPanel server.

The contact form does successfully send out eMails with the appropriate input fields displayed. Everything appears to be working as it should.

However, I can see in the error_log file the following notices (Undefined variable/index) associated with the php mailer file I am using to process the form. It appears that they are associated with the code that deals with $_POST or $_POST[$key] as follows:

PHP Notice:  Undefined variable: value in Mailer.php on line 17
PHP Notice:  Undefined variable: key in Mailer.php on line 17
PHP Notice:  Undefined index: submit in Mailer.php on line 29
PHP Notice:  Undefined index: name in Mailer.php on line 30
PHP Notice:  Undefined index: email in Mailer.php on line 31
PHP Notice:  Undefined index: telephone in Mailer.php on line 32
PHP Notice:  Undefined index: message in Mailer.php on line 33
PHP Notice:  Undefined index: SendUrl in Mailer.php on line 34

Here are the corresponding lines of code in the mailer.php file:

Lines 16 & 17:

foreach($_POST as $key => $value);
$_POST[$key] = htmlentities($value);

Lines 29 thru 34:

$MtStatus = $_POST['submit'];
$MtName = $_POST['name'];
$MtEmail = $_POST['email'];
$MtPhone = $_POST['telephone'];
$MtMessageText = $_POST['message'];
$MtSpamCheck = $_POST['SendUrl'];

And here is the contact form code:

        <form name="contact" id="contact-form" method="post" action="Mailer.php" onSubmit = "return validate_form()">
          <div class="text-left">
            <label>Name <span class="color-red">*</span></label>
            <div class="row margin-b-20">
                <div class="col-md-7 col-md-offset-0">
                    <input type="text" name="name" id="name" placeholder="Name" tabindex="1" class="form-control txtinput">
                </div>                
            </div>
            
            <label>Email <span class="color-red">*</span></label>
            <div class="row margin-b-20">
                <div class="col-md-7 col-md-offset-0">
                    <input type="email" name="email" id="email" placeholder="Email address" tabindex="2" class="form-control txtinput">
                </div>                
            </div>
            
            <label>Phone <span class="color-red">*</span></label>
            <div class="row margin-b-20">
                <div class="col-md-7 col-md-offset-0">
                    <input type="tel" name="telephone" id="telephone" placeholder="Phone number" tabindex="3" class="form-control txtinput">
                </div>                
            </div>
            
            <label>Message <span class="color-red">*</span></label>
            <div class="row margin-b-20">
                <div class="col-md-11 col-md-offset-0">
                    <textarea name="message" id="message" placeholder="Message..." tabindex="4" rows="8" class="form-control txtinput"></textarea>
                </div>                
            </div>

            <div>  
                <INPUT TYPE="hidden" NAME="SendUrl" class="SendUrlClass" value="" /> 
            </div>

           
            <div class="text-center">
              <p><input rel="hover-shadow" type="submit" name="submit" id="submitbtn" tabindex="5" class="btn-u btn-lg text-uppercase g-font-weight-700 g-font-size-13 rounded-0 g-px-25 g-py-15 mb-0 text-center" value="Send Message"></p>
            </div>
          </div>    
        </form>

As I said, the form works correctly. I am just seeing these 'PHP Notice' messages appear in the error_log.

Can anyone shed any light as to why these notices are showing up? Do I have a logic error in my code?

Thanks for any help!

~SunnyOz

SunnyOz
  • 543
  • 2
  • 10
  • 25
  • Since this post has been marked as duplicate, I can't provide my own answer properly. So I will post it here: I now see that the issue wasn't $_POST, but the two undefined variables: $key & $value. There are a lot of complicated ways of checking if they exist before using, but since I am not a pro at PHP, I prefer to keep it simple. Since I know that these variables will just be filled with text input from the contact form, I just set their initial values before the foreach loop - as follows: $key = ""; $value = ""; Now, no more error_log notices, and everything works fine! – SunnyOz Feb 11 '23 at 05:58

0 Answers0