Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Additional Field in Simple PHP Mail Form
Old 08-03-2009, 12:52 PM Additional Field in Simple PHP Mail Form
EJK
Average Talker

Posts: 16
Name: Eric
Trades: 0
Hey guys, here is my code right now for a simple sendmail form. I want to add an addtional field but am having a hell of a time doing it. My 3 required variables work fine, but I can't for the life of me add an additional variable to be sent in the email. All I'm tryign to do is add a telephone number field. you can check out the form here http://www.alertpc.net/dave4/repair-contacts.html



Code:
 
<?php
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];


/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
}
/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail('my email goes here',$subject,$message,$email)) {
  echo "<h4>Thank you for contacting uspair.  Your email has been sent.</h4>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>
EJK is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-03-2009, 01:55 PM Re: Additional Field in Simple PHP Mail Form
Novice Talker

Posts: 13
Trades: 0
Here you go, could you tell me why you thought this is hard to do?

(Just add a telephone field to your form now)

PHP Code:
<?php
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email $_POST['email'];
$subject $_POST['subject'];
$message $_POST['message'];
$telephone $_POST['telephone'];

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"$email)) {
  echo 
"<h4>Invalid email address</h4>";
  echo 
"<a href='javascript:history.back(1);'>Back</a>";
} elseif (
$subject == "") {
  echo 
"<h4>No subject</h4>";
  echo 
"<a href='javascript:history.back(1);'>Back</a>";
}

if (!empty(
$telephone)) {
    
$message .= "\r\n\r\nTelephone: " $telephone;
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail('my email goes here',$subject,$message,$email)) {
  echo 
"<h4>Thank you for contacting uspair.  Your email has been sent.</h4>";
} else {
  echo 
"<h4>Can't send email to $email</h4>";
}
?>
premiumscripts is offline
Reply With Quote
View Public Profile
 
Old 08-03-2009, 02:55 PM Re: Additional Field in Simple PHP Mail Form
EJK
Average Talker

Posts: 16
Name: Eric
Trades: 0
Can you please explain what exactly this line of code does?

Code:
 
if (!empty($telephone)) {
    $message .= "\r\n\r\nTelephone: " . $telephone;
}
As it is right now it doesn't return anythign to my email address. And I recieve no output. If I add this code:

Code:
if (!empty($telephone)) {
    $message .= "\r\n\r\nTelephone: " . $telephone;
 echo "<h4>Telephone Error</h4>";
}
it does return "Telephone Error" on the page. I am not an expert by any means, which is why I've come here for guidance.
EJK is offline
Reply With Quote
View Public Profile
 
Old 08-03-2009, 03:18 PM Re: Additional Field in Simple PHP Mail Form
Junior Talker

Posts: 4
Name: Christian South
Trades: 0
@EJK - What that is doing is adding in some blank lines and then added the telephone number to the bottom of the message. var_dump the post variable to make sure that the value is actually there.
xistins is offline
Reply With Quote
View Public Profile
 
Old 08-03-2009, 03:23 PM Re: Additional Field in Simple PHP Mail Form
Novice Talker

Posts: 13
Trades: 0
You shouldn't add the telephone error message there. In fact, I don't think a telephone nr is a required field is it? So there should be no error output, ever. Remove that line.

Anyway, it's my mistake. This should now work: (Simply a case of not having spotted the elseif)

PHP Code:
<?php
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email $_POST['email'];
$subject $_POST['subject'];
$message $_POST['message'];
$telephone $_POST['telephone'];

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"$email)) {
  echo 
"<h4>Invalid email address</h4>";
  echo 
"<a href='javascript:history.back(1);'>Back</a>";
} elseif (
$subject == "") {
  echo 
"<h4>No subject</h4>";
  echo 
"<a href='javascript:history.back(1);'>Back</a>";
} else {
  if (!empty(
$telephone)) {
       
$message .= "\r\n\r\nTelephone: " $telephone;
  }
  
/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
  
if (mail('my email goes here',$subject,$message,$email)) {
    echo 
"<h4>Thank you for contacting uspair.  Your email has been sent.</h4>";
  } else {
    echo 
"<h4>Can't send email to $email</h4>";
  }
}
?>
premiumscripts is offline
Reply With Quote
View Public Profile
 
Old 08-03-2009, 04:09 PM Re: Additional Field in Simple PHP Mail Form
EJK
Average Talker

Posts: 16
Name: Eric
Trades: 0
Thank you very much xistins and premium scripts. The reason I put the that extra line of code is to see where it actually stopped since I wasn't getting a return.

I'll be able to take the this bit of code and run with it. I really appreciate all the help you guys have given me!
EJK is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Additional Field in Simple PHP Mail Form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17241 seconds with 12 queries