views:

61

answers:

2

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="" />
+2  A: 

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'
);
azatoth
Thanks. Yeah I had just been reading that, but what confuses me about it is that the other, existing fields/variables do not do that..? Why would they work?
thatryan
@thatryan updated answer to explain more
azatoth
Thanks for the update. Can you check the OP I updated it with some HTML code. I got all the fields I believe, so I don't see why they would not get set...
thatryan
@thatryan if given input field is empty, that field will not be present in the query
azatoth
But none of the fields are empty..
thatryan
Additionally, using the isset() gets rid of the error yes, but still does not populate the variable. I don't understand why it wont come through...
thatryan
+1  A: 

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?

Patrick Daryll Glandien
Thanks, I am pretty sure I got all the fields though... I updated original post with some of the HTML
thatryan