0

I have a ajax driven contact form on my site linked here

I'm having 2 issues with it.

First being, I'm not sure why the error checking for the name field isn't working, my code seems to be correct (logic wise) but it still doesn't fire off.

Second, I have no idea how I can add more fields into the form. For example, adding Company Name for users to enter and send in the email. I tried adding the values like $company and the following code but I get this error that I can't pass that many variables into $mail :/ Trying to learn via trial and error, any help is appreciated!

here is my code:

<?php

include dirname(dirname(__FILE__)).'/mail.php';

error_reporting (E_ALL ^ E_NOTICE);

$post = (!empty($_POST)) ? true : false;

if($post)
{
  include 'email_validation.php';

  $name = stripslashes($_POST['name']);
  $email = trim($_POST['email']);
  $subject = 'Contact Form: Inquiry from corporate website';
  $message = stripslashes($_POST['message']);

  $error = '';

  // Check name

  if(!$name || strlen($name) < 1)
  {
    $error .= 'Please enter your name.<br />';
  }

  // Check email

  if(!$email)
  {
    $error .= 'Please enter an e-mail address.<br />';
  }

  if($email && !ValidateEmail($email))
  {
    $error .= 'Please enter a valid e-mail address.<br />';
  }

  // Check message (length)

  if(!$message || strlen($message) < 10)
  {
    $error .= "Please enter your message. It should have at least 10 characters.<br />";
  }


  if(!$error)
  {
    $mail = mail(CONTACT_FORM, $subject, $message,
     "From: ".$name." <".$email.">\r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());


    if($mail)
    {
      echo 'OK';
    }

  }
  else
  {
    echo '<div class="notification_error">'.$error.'</div>';
  }

}
?>

Here is my html form connecting to the php:

<form id="contactForm" action="">
  <input type="text" name="name" value="Name *" title="Name *" />
  <input type="text" name="email" value="Email *" title="Email *" />

  <textarea name="message" id="message" title="Message *">Message *</textarea>

  <div class="clear"></div>

  <input type="reset" class="btn btn_clear" value="Clear form" />
  <input type="submit" class="btn btn_blue btn_send" value="Send message!" />
  <div class="clear"></div>
</form>
Nick Andriopoulos
  • 10,313
  • 6
  • 32
  • 56
Spaceliving
  • 31
  • 1
  • 15
  • 1
    to better your code writing - drop the `?>` at the end of PHP-only files. It's the cause of loads of bad things, better described in [another SO post](http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag). – Nick Andriopoulos Apr 23 '13 at 16:04
  • It's not a good habit to ask 2 questions at once. Remove the other question and make a new post for it. – Jonast92 Apr 23 '13 at 16:07
  • ok well, any idea on what i have posted on this page already? havent helped much but hate on how i ask a question and how i format my text :/ – Spaceliving Apr 23 '13 at 16:12

2 Answers2

0

Concerning the validation of the name:

Don't do

if(!$name || strlen($name) < 1)
{
    $error .= 'Please enter your name.<br />';
}

Do this instead:

if((!(isset($_POST['name'])) || strlen($name) < 1)
{
    $error .= 'Please enter your name.<br />';
}

strlen won't do much if $name is undefined. But you should actually do this before you try to do strlen of the posted name.

Also, you can't add

$company

into the

$mail

function because you are most likely using it as an argument into the mail function, instead of appending it to the $message variable.

Also you should indent your code, It's horrible to read.

Jonast92
  • 4,964
  • 1
  • 18
  • 32
0

this is what is being put in mail.php, thats why there is a CONTACT_FORM

<?php
   // Where will you get the forms' results?
      define("CONTACT_FORM", 'dannyisaninja@gmail.com');
?>
Spaceliving
  • 31
  • 1
  • 15