Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP form (simple question) ..

Posted on 2004-03-30
14
Medium Priority
?
308 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:JamesBrownIII
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 14

Expert Comment

by:ThG
ID: 10714096
0
 
LVL 6

Expert Comment

by:Andy
ID: 10714866
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
 

Author Comment

by:JamesBrownIII
ID: 10715244
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:Andy
ID: 10715666
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
 

Author Comment

by:JamesBrownIII
ID: 10718135
thanks a lot Reapz.

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

0
 
LVL 6

Expert Comment

by:Andy
ID: 10718250
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
 

Author Comment

by:JamesBrownIII
ID: 10718285
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
 

Author Comment

by:JamesBrownIII
ID: 10722995
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
 
LVL 6

Expert Comment

by:Andy
ID: 10724061
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
 

Author Comment

by:JamesBrownIII
ID: 10726821
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
 
LVL 6

Expert Comment

by:Andy
ID: 10727604
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
 

Author Comment

by:JamesBrownIII
ID: 10727872
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
 
LVL 6

Accepted Solution

by:
Andy earned 120 total points
ID: 10728026
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
 

Author Comment

by:JamesBrownIII
ID: 10728175
EXCELLENT !!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question