Jump to content

Recommended Posts

First off, I am new to PHP, and want to learn it, but it seems very complicated. Where do I begin?

 

I found a script online for a contact form and I am having trouble making it work for my needs. I wanted to include a phone number field in the email message. This is what I have so far for the PHP, but it does not include the phone field in the body of the message. Everything else works fine. Apologize in advance if this message has been posted many times before.

 

<?php

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

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

$name = stripslashes($_POST['contactname']);
$email = trim($_POST['email']);
$subject = "New Message from your website"; 
$message = stripslashes($_POST['message']);
$phone = stripslashes($_POST['phone']);



$error = '';

// Check name

if(!$name)
{
$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) < 15)
{
$error .= "Please enter your message. It should have at least 15 characters.<br />";
}


if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $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>';
}

}
?>

 

 

This is because you are only passing in the $message var in your mail() function.  $message only has the contents of $_POST[message]

 

You need to do something like:

 

$message = $_POST['message'];

$message .= '/n/n';

$message .= $phone;

Thanks for the quick reply....that makes total sense to me now, and sure enough it works.

 

you added $message .= '/n/n'; what exactly is this supposed to do?

 

How would one go about styling it further to say  $message= The customer's phone number is =$phone;

 

Thanks for the quick reply....that makes total sense to me now, and sure enough it works.

 

you added $message .= '/n/n'; what exactly is this supposed to do?

 

How would one go about styling it further to say  $message= The customer's phone number is =$phone;

 

$message .= "Customer Phone Number is:" .$phone;

 

is there a way to force this its own line of the email.

 

this has been really great...thanks for the help just one more question

 

adding this

$message = stripslashes($_POST['message']);
$message .= "\n\n";
$message = "Customer Phone Number is:" .phone;

 

seems to loose the validation for the message. If I dont put anything in the message field, it does not check and sends anyway.

 

// Check message (length)

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


if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $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>';
}

}

 

 

$message = "Customer Phone Number is:" .phone;

 

Should be:

 

$message .= "Customer Phone Number is: " . $phone;

 

Why?

 

1. The '$' denotes a variable.  Without it, PHP thinks that you have a named constant titled 'phone' that you're trying to add to your $message string.

 

2. The '.' is the string concatenation operator.  It simply adds whatever value is to the right to the string on the left.  Think of it like addition.

 

3. The '=' is the assignment operator.  It takes the value on the right of it and assigns it to the variable on the left.

 

4. '.=' is a shorthand concatenation and assignment operator.  You could rewrite your statement as:

 

$message = $message . "Customer Phone Number is " . $phone;

 

They're equivalent statements.  The shorthand is used because, well, it's shorter.

 

EDIT: So, what was the bad statement I corrected doing?  Since you were using the normal assignment operator rather than the shorthand, you were simply overwriting your $message with that statement.  However, your message still won't validate as you add your own strings to it before checking to see if a message exists at all, or if it's the right length.  Do the check first, then add your text.

Thanks for the reply and the explanation, I have a lot to learn.

 

but...

 

the form still does not validate the message. I take those 2 lines out and it validates it. I thought it might be because I am testing it offline in WAMP.

<?php


include 'config.php';

error_reporting (E_ALL ^ E_NOTICE);

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

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

$name = stripslashes($_POST['contactname']);
$email = trim($_POST['email']);
$subject = "New Message";
$message = stripslashes($_POST['message']);
$message .= "\n\n";
$message = $message . "Customer Phone Number is " . $phone;



$error = '';

// Check name

if(!$name)
{
$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) < 15)
{
$error .= "Please enter your message. It should have at least 15 characters.<br />";
}


if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $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>';
}

}
?>

 

functions include

 

<?php
function ValidateEmail($email)
{
/*
(Name) Letters, Numbers, Dots, Hyphens and Underscores
(@ sign)
(Domain) (with possible subdomain(s) ).
Contains only letters, numbers, dots and hyphens (up to 255 characters)
(. sign)
(Extension) Letters only (up to 10 (can be increased in the future) characters)
*/

$regex = '/([a-z0-9_.-]+)'. # name

'@'. # at

'([a-z0-9.-]+){2,255}'. # domain & possibly subdomains

'.'. # period

'([a-z]+){2,10}/i'; # domain extension 

if($email == '') { 
return false;
}
else {
$eregi = preg_replace($regex, '', $email);
}

return empty($eregi) ? true : false;
}
?>

Replace your first code snippet with the following.  Take note of what I do to $message, and how that matches what I said two posts ago.

 

<?php
   error_reporting(E_ALL ^ E_NOTICE);

   include 'config.php';

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

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

      $name = stripslashes($_POST['contactname']);
      $email = trim($_POST['email']);

      $error = '';

      // Check name

      if(!name)
      {
         $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) < 15)
      {
         $error .= "Please enter your message. It should have at least 15 characters.<br />";
      }

      if(!$error)
      {
         $subject = "New Message";
         $message = stripslashes($_POST['message']);
         $message .= "\n\n";
         $message .= "Customer Phone Number is " . $phone;

         $mail = mail(WEBMASTER_EMAIL, $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>';
      }
   }
?>

 

Also, where does $phone come from?  Right now, you don't have any values being placed in that variable.  Do you have a corresponding HTML form field for the phone number?  If so, you need to grab a hold of that value in your script.

phew, it works. I need to revisit, the differences in your code.

 

Also, where does $phone come from?  Right now, you don't have any values being placed in that variable.  Do you have a corresponding HTML form field for the phone number?  If so, you need to grab a hold of that value in your script.

 

I do have the 'phone' value in the form, even though the phone variable is being inserted in the message, do I still need to define it, under the lists of variables in the top?

Yup.  You should have:

 

$phone = $_POST['phone'];

 

You should validate it as well.  A regex should do the trick.

 

EDIT: Just a quick word about form inputs and how they work with PHP -

 

Input values are stored in one of two superglobal arrays - $_GET or $_POST (there's also a catch-all $_REQUEST, but that's sloppy and potentially dangerous to use).  Which array is used depends on what method your HTML form will use.  $_GET for get, $_POST for post.

 

When you want to access a form value, you do it by accessing the superglobal array with the name of the form input you want to retrieve.  So, $_POST['phone'] assumes that your form used the post method and had an input with the name of phone.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.