Question

I have a problem with my contact form, I got this message "Notice: Undefined index: contact_name in...". This is basically the code I'm using.

<?php
                    //Receive all the data to process
                    $contact_name = $_POST['contact_name'];
                    $contact_company = $_POST['contact_company'];
                    $contact_country = $_POST['contact_country'];
                    $contact_phone = $_POST['contact_phone'];
                    $contact_email = $_POST['contact_email'];
                    $contact_subject = $_POST['contact_subject'];
                    $contact_message = $_POST['contact_message'];
                    $contact_headers = 'Sent by: '.$contact_name.' ('.$contact_email.');'.' Phone: '.$contact_phone.'; Country: '.$contact_country.'; Company: '.$contact_company;
                    $contact_to = 'name@email.com';

                    if($contact_name != '' || $contact_country != '' || $contact_email != '' || $contact_message != ''){  
                        mail($contact_to, $contact_subject, $contact_message, $contact_headers); //calling php mail function
                        echo 'Thank you for contacting us.<br><br><a href="contact.php">Go back.</a>';
                    }else{  
                        echo 'There was an error sending the message.<br><br><a href="index.php">Go back.</a>';  
                    }
                ?>

This is the HTML

<form id="main-contact-form" action="contact.php" method="post">
                    <p>
                        <label for="contact_name" class="label-s required">NAME</label>
                        <input type="text" role="form-item" name="contact_name" id="contact_name" required>
                    </p>
                    <p>
                        <label for="contact_company" class="label-s">COMPANY</label>
                        <input type="text" role="form-item" name="contact_company" id="contact_company">
                    </p>
                    <p>
                        <label for="contact_country" class="label-s required">COUNTRY</label>
                        <input type="text" role="form-item" name="contact_country" id="contact_country" required>
                    </p>
                    <p>
                        <label for="contact_phone" class="label-s">PHONE</label>
                        <input type="text" role="form-item" name="contac_phone" id="contact_phone">
                    </p>
                    <p>
                        <label for="contact_email" class="label-s required">EMAIL</label>
                        <input type="email" role="form-item" name="contact_email" id="contact_email" required>
                    </p>
                    <p>
                        <label for="contact_subject" class="label-s">SUBJECT</label>
                        <input type="text" role="form-item" name="contact_subject" id="contact_subject">
                    </p>
                    <p>
                        <label for="contact_message" class="label-s required">MESSAGE</label>
                        <textarea role="form-item" name="contact_message" id="contact_message" class="avoid-resize-h" required></textarea>
                    </p>
                    <input id="contact-submit" type="submit" role="btn" class="btn-primary btn-l expand" value="SEND">
                </form>

When I use this placing the php code in a different file and changing the action, it works, but I'm trying to use it placing the php code in the same file and there's when I get the error message.

I hope you can help me. Thank you.

Was it helpful?

Solution

You are not checking if $_POST is set when they are in the same file and it is an empty array. Add an if statement to check for $_POST

<?php
       if($_POST) {
            //Receive all the data to process
            $contact_name = $_POST['contact_name'] ? : '';
            $contact_company = $_POST['contact_company'] ? : '';
            $contact_country = $_POST['contact_country'] ? : '';
            $contact_phone = $_POST['contact_phone'] ? : '';
            $contact_email = $_POST['contact_email'] ? : '';
            $contact_subject = $_POST['contact_subject'] ? : '';
            $contact_message = $_POST['contact_message'] ? : '';
            $contact_headers = 'Sent by: '.$contact_name.' ('.$contact_email.');'.' Phone: '.$contact_phone.'; Country: '.$contact_country.'; Company: '.$contact_company;
            $contact_to = 'name@email.com';

            if($contact_name != '' || $contact_country != '' || $contact_email != '' || $contact_message != ''){  
                  mail($contact_to, $contact_subject, $contact_message, $contact_headers); //calling php mail function
                  echo 'Thank you for contacting us.<br><br><a href="contact.php">Go back.</a>';
            }else{  
                  echo 'There was an error sending the message.<br><br><a href="index.php">Go back.</a>';  
            }
      }
?>

You should also make sure that you actually have values, the ternary operator is useful for this.

http://us1.php.net/manual/en/function.mail.php

Your headers option in the mail function is not correct. That should be an array, see the example in the manual. It should be:

$contact_headers = 'From: webmaster@example.com';

What you have in your $contact_headers variable should probably get appended to your message. The headers are for your server to send the mail.

OTHER TIPS

Be sure to check that $_POST data is set, otherwise the first time a user visits it will be empty and you'll see the undefined index errors like that.

Wrap the form-related php code with:

if( !empty($_POST) ){

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top