0

I'm just trying to clean up these error logs. I have a form on my site, that WORKS. But my error logs are still showing the undefined indexes. I checked this question, PHP - Notice: Undefined index:, and I think it's what I need but I'm not sure how to implement it to stop sending errors. Again, the form does work, so then why am I getting these errors and how do I stop them.

Note*: If it's a duplicate then comment me, please don't mark it. I see this question as being different since it's not preventing my code from working. I just want to get rid of the errors because it makes no sense how the error code works.

Errors:

[14-Sep-2015 14:40:41 UTC] PHP Notice:  Undefined variable: MSG in /home/seenserv/public_html/contact.php on line 134
[14-Sep-2015 14:40:41 UTC] PHP Warning:  implode(): Invalid arguments passed in /home/seenserv/public_html/contact.php on line 47
[14-Sep-2015 14:40:41 UTC] PHP Notice:  Undefined index: name in /home/seenserv/public_html/contact.php on line 138
[14-Sep-2015 14:40:41 UTC] PHP Notice:  Undefined index: phone in /home/seenserv/public_html/contact.php on line 139
[14-Sep-2015 14:40:41 UTC] PHP Notice:  Undefined index: email in /home/seenserv/public_html/contact.php on line 140
[14-Sep-2015 14:40:41 UTC] PHP Notice:  Undefined index: invoice in /home/seenserv/public_html/contact.php on line 141
[14-Sep-2015 14:40:41 UTC] PHP Notice:  Undefined index: comments in /home/seenserv/public_html/contact.php on line 142

FORM:

<form method="post" action="">
                <input type="text" name="name" placeholder="&ast;Name" value="<?php echo $_POST['name']; ?>">
                <input type="tel" name="phone" placeholder="&ast;Phone Number" value="<?php echo $_POST['phone']; ?>">
                <input type="email" name="email" placeholder="&ast;Email" value="<?php echo $_POST['email']; ?>">
                <input type="text" name="invoice" placeholder="Invoice Number &lpar;optional&rpar;" value="<?php echo $_POST['invoice']; ?>">
                <textarea name="comments" maxlength="500" rows="10" cols="10" placeholder="&ast;Please enter your comments here..."><?php echo htmlentities($_POST['comments'], ENT_COMPAT,'ISO-8859-1', true);?></textarea>
                <button type="submit">Submit</button>
            </form> 

PHP Form functions:

    if(!array_filter($MSG)){
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $MSG[] = "Invalid Email Format (test@provider.com)";
        }
        else{
            $POST['invoice'] = $invoice['invoice'];
            if(send_mail($POST)){
                header('Location: messageSent.php');
            }
            else{
                $MSG[] = "Email Failed. Please Try Again.";
            }
        }
    }
}

function filter_post($POST){
    $keys = array('name','phone','email','invoice','comments');
    $POST = array_intersect_key($POST, array_flip($keys));
    $POST = array_map('strip_tags', $POST);
    return($POST);
}

function check_empty($POST){
    foreach($POST as $key => $value){
        if(empty($value)){
            $MSG[] = "You need to fill out the $key section";
        }
    }
    return($MSG);
}
function send_mail($POST){
    extract($POST);
    $to = '7servicerepairs@gmail.com';
    $sbj = 'New Question For Se7en Service!';
    $msg = "Name: $name \n Phone: $phone \n Email: $email \n Invoice #: $invoice \n Comments: $comments";
    $headers = "From: Se7en Service Customer Care <7servicerepairs@gmail.com>\r\n";
    $headers .="Reply-To: $email";
    return(mail($to, $sbj, $msg, $headers));
}
function output_errors($MSG){
    return '<ul><li>' . implode('</li><li>', $MSG) . '</li></ul>';  
}
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>
Community
  • 1
  • 1
blackRob4953
  • 1,585
  • 2
  • 12
  • 15
  • Have a read on superglobals http://php.net/manual/en/language.variables.superglobals.php then compare it with what you're using. – Funk Forty Niner Sep 14 '15 at 21:07
  • This is because when you load the form without be coming from a post, the variable $ _POST is empty. Then his indices such as email, name etc. does not exist (Undefined index) – Beto Sep 14 '15 at 21:39
  • @Beto ok that makes sense, how do I work around that? – blackRob4953 Sep 14 '15 at 21:40
  • Before de form, check if the variable its defined. `if(!$_POST['name']) $_POST['name'] = '';` for each input – Beto Sep 14 '15 at 21:42
  • @Beto post as an answer. I'll be home soon to test it and if it works I'll approve. Sounds like it will tho – blackRob4953 Sep 14 '15 at 21:53
  • I cant post answers in your question because it has been marked as duplicate – Beto Sep 14 '15 at 21:59
  • @Fred-ii- can you please remove the duplicate so the user Beto can post his answer. It worked for me in this case. Simply fix without the complications from the other duplicate posts you mentioned. I feel his answer will help the public easier, hoping to prevent duplicate questions like this from arising... – blackRob4953 Sep 14 '15 at 22:13

0 Answers0