thatryan
thatryan

Reputation: 1559

PHP mail keeps giving me undefined index, help?

I have a contact form script, and I added a few fields to it, identical to the rest, but the new variables on submit give 'notices' of undefined index, and the variables do not populate. I grab them POST like this,

$name     = $_POST['name'];
$website  = $_POST['website'];
$company  = $_POST['company'];
$addy     = $_POST['addy'];
$email    = $_POST['email'];
$phone    = $_POST['phone'];

The name, email and phone all work, but website, addy and company do not.

The JavaScript that grabs it looks like so,

$.post(action, { 
        name: $('#name').val(),
        email: $('#email').val(),
        website: $('#website').val(),
        company: $('#company').val(),
        addy: $('#addy').val(),
        phone: $('#phone').val(),

..etc

What can cause this? I am unsure what other code would be helpful, thank you.

EDIT TO ADD HTML.

Here is some of the form code in the HTML, I can not see anything I missed..

<form method="post" action="send_mail.php" name="contactform" id="contactform">

        <fieldset>

        <legend>Please fill in the following form to contact us</legend>

        <label for=name accesskey=U><span class="required">*</span> Your Name</label>
        <input name="name" type="text" id="name" size="30" value="" /> 

        <br />
        <label for=email accesskey=E><span class="required">*</span> Email</label>
        <input name="email" type="text" id="email" size="30" value="" />

        <br />
        <label for=website accesskey=W>Website</label>
        <input name="website" type="text" id="website" size="30" value="" />

        <br />
        <label for=company accesskey=C>Company</label>
        <input name="company" type="text" id="company" size="30" value="" />

        <br />
        <label for=addy accesskey=A>Address</label>
        <input name="addy" type="text" id="addy" size="30" value="" />

Upvotes: 2

Views: 654

Answers (2)

Patrick Glandien
Patrick Glandien

Reputation: 7851

jQuery does not send post fields whose value is null. So in your case the error is probably that you don't have fields with the given IDs website, addy, ....

Maybe you forgot to add the id=".." attribute or forgot to change it when copying the old code?

Upvotes: 1

azatoth
azatoth

Reputation: 2379

If the values is false or empty, the post won't include those variables in the AJAX call. Recommend you to use isset() in the PHP to verify the variables are set:

$name     = isset($_POST['name']) ? $_POST['name'] : '';
etc...

Detailed explanation

If we in javascript (jQuery) has following code:

$.post('url',{
  'foo': 'bar',
  'baz': $('#nonexisting_element').val(),
   'qux': 'quux'
},...);

An #nonexisting_element doesn't exists, it will send following:

foo=bar&qux=quux

and on the PHP side following will end up in the PHP array:

$_POST = array (
    'foo' => 'bar'
    'qux' => 'quux'
);

Upvotes: 2

Related Questions