Link to home
Start Free TrialLog in
Avatar of JamesBrownIII
JamesBrownIII

asked on

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
Avatar of Giovanni G
Giovanni G
Flag of Italy image

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);
Avatar of JamesBrownIII
JamesBrownIII

ASKER

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-
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>
thanks a lot Reapz.

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

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. :)
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 :)
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.
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>
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) { ?>




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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Andy
Andy
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
EXCELLENT !!