PHP form (simple question) ..

Hi there,

I have a simple html form with the input fields and text area (as follows):

Name:

Email:

Subject:

Message:


I just want to extract the data from those fields and send it compiled as a presentable e-mail to an e-mail address.


Thanks for your time
JamesBrownIIIAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThGCommented:
0
AndyCommented:
After you submit the form you get the values in those textboxes...

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject];
$message = $_POST['message'];

If you want you can add any code to validate the form here...

Then send the email like so...

$to = "you@yourdomain.com";
$body = "$message"; // If you don't want to add anything else to the message you could just do $body = $_POST['message']; above
$header = "From: $email";
mail($to, $subject, $body, $header);
0
JamesBrownIIIAuthor Commented:
Thanks Reapz.

so would that php script be in the same page as the form? & would it HAVE to be called contact.php or can u call it contact.html?

-JB3-
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

AndyCommented:
Pages containing PHP should have the .php extension. you can submit the form to another page or the same page it's up to you.

Here's an example that submits the form to the same page. It also shows how to validate the form...

<?php
$option = $_GET['option'];
//Set recipient email
$myemail = "you@yourdomain.com";
//See if the form has been submitted
if($option == "send"){      
      //Assign the text fields to variables
      $name = $_POST['name'];
      $email = $_POST['email'];
      $subject = $_POST['subject'];
      $message = $_POST['message'];
      
      //Now to validate the form
      $errors = array();
      if($name == ""){
            array_push($errors, "You have not entered your name!");
      }
      if($email == ""){
            array_push($errors, "You did not enter your email address!");
      }
      else if(ereg("[A-Za-z0-9_-]+([.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([.]{1}[A-Za-z0-9-]+)+", $email)) {
            $emailadd = "valid";
      }
      else {
            $emailadd = "invalid";
      }
      if ($emailadd == "invalid") {
            array_push($errors, "That does not appear to be a valid email address!");
      }
      if($subject == ""){
            array_push($errors, "You did not enter a subject heading!");
      }
      if($messbody == ""){
            array_push($errors, "You did not enter a message!");
      }
      //If there are no errors then send the email
      if(count($errors) == 0){
            $to = "$myemail";
            $body = "$message";
            $header = "From: $email";
            mail($to, $subject, $body, $header);
      }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php //If there were no errors tell user email has been sent
if($option == "send" && count($errors) == 0){ ?>
      <table width="500" border="0" align="center" cellpadding="3" cellspacing="3">
            <tr>
            <td colspan="3">
            <p class="style1">Your message has been sent successfully!</p>
            <p class="style1">I will get back to you as soon as possible. </p></td>
            </tr>
      </table>
<?php }
//If there were errors tell them what they were
elseif($option == "send" && count($errors) > 0) { ?>
      <table width="500" border="0" align="center" cellpadding="3" cellspacing="3">
            <tr>
          <td colspan="3"><p class="style1">The following errors occured...</p>
              <?php foreach($errors as $e){ ?>
              <strong><?php echo $e; ?><br>
              </strong>
              <?php } ?>
              <p>Please click the 'Back' button on your browser to correct the mistakes.</p></td>
        </tr>
      </table>
<?php }
//Or if the form hasn't been submitted ie: just opened then display the form
else { ?>
<table width="500" border="0" align="center" cellpadding="3" cellspacing="3">
  <tr>
      <td colspan="2"><p align="justify">Please use the following form to contact me by email.</p></td>
  </tr>
  <form name="form1" method="post" action="?option=send">
      <tr>
        <td width="150"><strong>Name:</strong></td>
        <td>
            <input name="name" type="text" id="name" size="40">
      </td>
      </tr>
      <tr>
        <td width="150"><strong>Email Address: </strong></td>
        <td><input name="email" type="text" id="email" size="40"></td>
      </tr>
      <tr>
        <td width="150"><strong>Subject:</strong></td>
        <td><input name="subject" type="text" id="subject" size="40"></td>
      </tr>
      <tr>
        <td width="150" valign="top"><strong>Message:</strong></td>
        <td><textarea name="messbody" cols="30" rows="5" id="messbody"></textarea></td>
      </tr>
  </form>
  <tr>
      <td width="150">&nbsp;</td>
      <td> <input type="submit" name="Submit" value="Submit">
    <input type="reset" name="Submit2" value="Reset"> </td>
  </tr>
  <tr>
                <td colspan="2"><strong><br>
  </strong>If you would prefer to use your email client click <a href="mailto:<?php echo $myemail; ?>">here</a>. </td>
  </tr>
</table>
<?php } ?>
</body>
</html>
0
JamesBrownIIIAuthor Commented:
thanks a lot Reapz.

what exactly does this do : action="?option=send"

0
AndyCommented:
When you submit a form the action attribute specifies where you are submitting it too.
If I was going to process the data on a second page I would have just used say...

action="contactpart2.php"

Because I am processing on the same page I need to let the code know that the form has been submitted and not just to load the form up again as normal.

? means the start of the variables in the URL. You can include more than one but must separate them with an & symbol. For example...

somepage.php?name=andy&location=here

Anyway, in the above example I have just used "?option=send" and because I have no newpage before the ? the code submits the form to the same page. 'Option' is just what I called the variable and 'send' is its value. I retrieve that value with $_GET['option']. The value will only be 'send' when the form has been submitted, so if it is, the code now knows to process your form data. If 'option' was empty then the form would just be displayed ready for input.

There are other ways to do this but I do it this way because I generally have a lot of possible options on my pages and it's habit and makes sense in my head. :)
0
JamesBrownIIIAuthor Commented:
yea I see now, thanks alot, I will award you the points but I am yet to implement this script so I will leave this thread open incase I have any more questions, again thanks for that :)
0
JamesBrownIIIAuthor Commented:
Hey Reapz.

The validation works but I get this error on load:

Notice: Undefined index: option in c:\program files\apache group\apache\htdocs\contact1.php on line 2


AND then testing locally I get this error:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in c:\program files\apache group\apache\htdocs\contact1.php on line 43

How can I correct these?

Thanks mate.
0
AndyCommented:
Testing the page locally will generate that error because you have no SMTP server on your local machine. Don't worry about it as long as it works on your webspace.

Not sure why you're getting that other error it work's fine for me but I did notice a couple of typos in there. Anyway lets try another way. This time I've got rid of the 'option' bit and I'm simply going to check if the 'submit' button has been pressed...

<?php
//Set recipient email
$myemail = "andy@aetechsystems.co.uk";
//See if the form has been submitted
if($_POST['Submit']){    
     //Assign the text fields to variables
     $name = $_POST['name'];
     $email = $_POST['email'];
     $subject = $_POST['subject'];
     $message = $_POST['message'];
     
     //Now to validate the form
     $errors = array();
     if($name == ""){
          array_push($errors, "You have not entered your name!");
     }
     if($email == ""){
          array_push($errors, "You did not enter your email address!");
     }
     else if(ereg("[A-Za-z0-9_-]+([.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([.]{1}[A-Za-z0-9-]+)+", $email)) {
          $emailadd = "valid";
     }
     else {
          $emailadd = "invalid";
     }
     if ($emailadd == "invalid") {
          array_push($errors, "That does not appear to be a valid email address!");
     }
     if($subject == ""){
          array_push($errors, "You did not enter a subject heading!");
     }
     if($message == ""){
          array_push($errors, "You did not enter a message!");
     }
     //If there are no errors then send the email
     if(count($errors) == 0){
          $to = "$myemail";
          $body = "$message";
          $header = "From: $email";
          mail($to, $subject, $body, $header);
     }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php //If there were no errors tell user email has been sent
if($_POST['Submit'] && count($errors) == 0){ ?>
     <table width="500" border="0" align="center" cellpadding="3" cellspacing="3">
          <tr>
          <td colspan="3">
          <p class="style1">Your message has been sent successfully!</p>
          <p class="style1">I will get back to you as soon as possible. </p></td>
          </tr>
     </table>
<?php }
//If there were errors tell them what they were
elseif($_POST['Submit'] && count($errors) > 0) { ?>
     <table width="500" border="0" align="center" cellpadding="3" cellspacing="3">
          <tr>
          <td colspan="3"><p class="style1">The following errors occured...</p>
              <?php foreach($errors as $e){ ?>
              <strong><?php echo $e; ?><br>
              </strong>
              <?php } ?>
              <p>Please click the 'Back' button on your browser to correct the mistakes.</p></td>
        </tr>
     </table>
<?php }
//Or if the form hasn't been submitted ie: just opened then display the form
else { ?>
<table width="500" border="0" align="center" cellpadding="3" cellspacing="3">
  <tr>
     <td colspan="2"><p align="justify">Please use the following form to contact me by email.</p></td>
  </tr>
  <form name="form1" method="post" action="">
     <tr>
       <td width="150"><strong>Name:</strong></td>
       <td>
          <input name="name" type="text" id="name" size="40">
     </td>
     </tr>
     <tr>
       <td width="150"><strong>Email Address: </strong></td>
       <td><input name="email" type="text" id="email" size="40"></td>
     </tr>
     <tr>
       <td width="150"><strong>Subject:</strong></td>
       <td><input name="subject" type="text" id="subject" size="40"></td>
     </tr>
     <tr>
       <td width="150" valign="top"><strong>Message:</strong></td>
       <td><textarea name="message" cols="30" rows="5" id="message"></textarea></td>
     </tr>
    <tr>
     <td width="150">&nbsp;</td>
     <td><input type="submit" name="Submit" value="Submit">
      <input type="reset" name="Submit2" value="Reset"></td>
  </tr>
  </form>
  <tr>
    <td colspan="2"><strong><br>
  </strong>If you would prefer to use your email client click <a href="mailto:<?php echo $myemail; ?>">here</a>. </td>
  </tr>
</table>
<?php } ?>
</body>
</html>
0
JamesBrownIIIAuthor Commented:
OK I have tried this new script out but i get the following error on line 5, 154 & 166 :

Notice: Undefined index: submit in c:\program files\apache group\apache\htdocs\contact1.php on line 5

line 5=

if($_POST['submit']){

line 154 the one after =

if($_POST['submit'] && count($errors) == 0){ ?>

line 166 the one after =

elseif($_POST['submit'] && count($errors) > 0) { ?>




0
AndyCommented:
I think there must be something wrong with the coniguration of your PHP. Both work fine for me on my server. You should ask the server admin.
0
JamesBrownIIIAuthor Commented:
No sorry I tried it on a proper server and it all appears to work flawlessly.

Just one thing. It sends the email address as from: the subject as subject and message as message.
How would I get the name passed as from and the email included in the message?

Also can you explain to me what the heck this does:



else if(ereg("[A-Za-z0-9_-]+([.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([.]{1}[A-Za-z0-9-]+)+", $email)) {
          $emailadd = "valid";

thanks.
0
AndyCommented:
Lol... scarey isn't it?! All that does is check that to see if the email looks like an email.

You can put whatever you want as the From: header but if you don't put their email address you won't be able to reply and a lot of the time if it's not and email address it will tag the domain onto the end of it anyway.

To add stuff to the message body do something like this...

$body = "$name has just sent the following email from the form on your website...\n\n$message";

Where \n is a linebreak.

Hope that helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JamesBrownIIIAuthor Commented:
EXCELLENT !!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.