Jump to content

Recommended Posts

I have been trying to add additional fields to this form script but when sent to email address I cannot get anything extra to be sent to the email. If for example I add another field to the form and place it in the $mail line below ahead of the $comments attribute I receive that information but then not the $comments. If I change the order of the last two fields I then get the contents of $comments but not the additional field.

 

$mail = $smtp->send($to, $headers, $comments);

 

I know very little about PHP (easily guessed !!) but after searching everywhere and trying to get better examples I have hit the frustration of despair. I have included the full script below and would appreciate it if someone could show what needs to change to add an extra field to the script.

 

 

Script and Info

The code below is a sample php based contact form. It has been tested to work on our servers and can be customised quite easily. Take note, however, that we do NOT provide support for web design or development related queries and that this sample has been provided in good faith and on an 'as is' basis.

 

To use, just copy and paste the code below into a new file and call it "mail.php". Then upload it to your website and view the page.

 

You'll also need to edit the "to", "username" and "password" lines as noted by the comments to include the correct details.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Email Form Example</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<?php

 

//check if the submit button has been pressed.

$submit = $_GET['submit']; 

 

//if the submit button has been pressed, send the email

if ($submit == 'true')

{

 

require_once "Mail.php";

 

$from = $_POST[txtEmail];

$subject = $_POST[txtSubject];

$comments = $_POST[txtComments];

$to = "[email protected]";             // this is the email address to send the form results to

 

$host = "localhost";

$username = "[email protected]";      // replace with your email address hosted by MD Webhosting

$password = "password";                 // replace with the password for your email address

 

$headers = array ('From' => $from,

   'To' => $to,

   'Subject' => $subject);

 

$smtp = Mail::factory('smtp',

   array ('host' => $host,

     'auth' => true,

     'username' => $username,

     'password' => $password));

 

$mail = $smtp->send($to, $headers, $comments);

 

if (PEAR::isError($mail)) {

   echo("<p>" . $mail->getMessage() . "</p>");

  } else {

   echo("<p>Message successfully sent!</p>");

  }

 

}

 

// if the submit button has not been pressed, display the email form

 

else

 

{

?>

 

<table border="0">

<form action="mail.php?submit=true" method="post">

  <tr>

    <td>Your Email Address :</td>

    <td><input type="text" name="txtEmail"></td>

  </tr>

  <tr>

    <td>Subject :</td>

    <td><input type="text" name="txtSubject"></td>

  </tr>

<tr>

    <td>Comments :</td>

    <td><input type="text" name="txtComments"></td>

  </tr>

<tr>

<td> </td>

<td><input type="submit" value="Send"></td></tr>

 

   </table><br/>

 

</body>

</html> 

<?

}

?>

 

 

 

Please use code tags!

 

mail() is a function and takes certain arguments.  The arguments are where you are sending it to, where it's coming from, what you are sending, etc... if you want to add more stuff to the actual email content then you need to add it to the $content variable.

 

It would be greatly appreciated if someone could provide to me by editing sample below, the additional code require to add a form field and how it is sent to the email address. Everywhere I have searched via google does not seem to get beyond this basic type of example. A search on $content variable as suggested yields nothing that I can make head or tail of to help.

 

Thanks in advance

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Email Form Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<?php

//check if the submit button has been pressed. 
$submit = $_GET['submit'];  

//if the submit button has been pressed, send the email
if ($submit == 'true')
{

require_once "Mail.php";

$from = $_POST[txtEmail];
$subject = $_POST[txtSubject];
$comments = $_POST[txtComments];
$to = "[email protected]";             // this is the email address to send the form results to

$host = "localhost";
$username = "[email protected]";      // replace with your email address hosted by MD Webhosting
$password = "password";                 // replace with the password for your email address 

$headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);

$smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));

$mail = $smtp->send($to, $headers, $comments); 

if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }

}

// if the submit button has not been pressed, display the email form

else 

{ 
?>

<table border="0">
<form action="mail.php?submit=true" method="post">
  <tr>
    <td>Your Email Address :</td>
    <td><input type="text" name="txtEmail"></td>
  </tr>
  <tr>
    <td>Subject :</td>
    <td><input type="text" name="txtSubject"></td>
  </tr>
<tr>
    <td>Comments :</td>
    <td><input type="text" name="txtComments"></td>
  </tr>
<tr>
<td> </td>
<td><input type="submit" value="Send"></td></tr>

   </table>


</body>
</html>  
<?
}
?>

mail($to, $subject, $content);

 

The 3rd argument is where you put your email content.  $content is just a string that holds the content.  So if you want to add more stuff to the email....

 

$content = "something";

$content .= " and something else";

 

or

 

$content = "My name is " . $_POST['name'] . "\r\n";

$content .= "My address is " . $_POST['address'];

 

Basic string concatenation...

Thanks Crayon V but as I am not a developer and only need to resolve this single form and much of this has gaps for me in my knowledge. I am a heart surgeon actually and very good at that should you ever need such an operation.

Could you please copy the example code and modify it to show what needs to happen. Once I see it, I will get it and be able to keep going without really understanding what I am doing.

I learnt how to operate on hearts by watching videos and live examples of others doing it and can save lives even though I don't really know what I am doing ;D

 

If only. Anyway, thanks to your help and doing a little more digging in the Stringing Along part of this tutorial http://devzone.zend.com/node/view/id/625 I got to understand what you were on about and success has been achieved !! Cannot believe I have wasted so much of my life on something so trivial.

 

Anyway, back to the heart surgery, far easier than being a developer. Sure appreciate how you give such help so readily, I will do the same the day I can provide eSurgery ! Thanks again.

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.