Jump to content
msugi002

Form Info being sent to Email

Recommended Posts

Hello all,

 

I am new to PHP and I am trying to find a successful way to send the contents of a form as an e-mail to an administrator e-mail address. I've followed a few tutorials on youtube, tried to connect things to each other using my prior knowledge on programming, and ended up with the following:

 

(note: name of this file is "form.php", and I have changed information on this form for privacy reasons)

 

<?php
    if (isset($_POST['submitButton'])) {
        
        require 'phpmailer/PHPMailerAutoload.php';

        function sendemail($nm, $hp, $em, $mp, $ch, $mc){
            //create an instance of PHP Mailer
            $mail = new PHPMailer();
            //set a host
            $mail->host = "stmp.gmail.com";
            //enable STMP
            //$mail->isSTMP()  //note: this method may not always work.
            //$mail->SMTPDebug = 2;
            //set authentication to true
            $mail->STMPAuth = true;
            //Set login details for Gmail account.
            $mail->Username = "admin@company.com";
            $mail->Password = "Password";
            //Set types of protection
            $mail->STMPSecure = "ssl"; //or we can use TLS
            //set a port
            $mail->Port = 465; //or 587 if TLS

            //set content of e-mail
            $mail->setFrom(address: '$email', name: '$name');
            $mail->addAddress(address: 'admin@company.com', name: 'Name of the Administrator');
            $mail->Subject = 'New Contact';
            $mail->Body = "You've recieved a new contact! <br /></br />" . $nm . "<br />" . $em . "<br />" . $hp . "<br />" . $mp . "<br />" . $ch . "<br />" . $mc;
            $mail->isHTML(true);
            //send an email
            return $mail->send();
        }
        
        $name = $_POST['name'];
        $homephone = $_POST['homePhone'];
        $email = $_POST['email'];
        $mobilephone = $_POST['mobilePhone'];
        $children = $_POST['children'];
        $messagecontent = $_POST['messageContent'];

        return sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent));

        if (sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent)){
                $msg = "Mail was sent successfully";
            else
                $msg = "Woops, something went wrong. Please Try Again";
            };
    }
?>

<!DOCTYPE html>
<html>

  <head>
    <title>Signup Form</title>
  </head>

  <body>
        <form action="form.php" method="post">
          <label>Name:</label>
          <input type="text" name="name" />

          <label>Home Phone:</label>
          <input type="text" name="homePhone" />
          
          <label>Email:</label>
          <input type="text" name="email" />

          <label>Mobile Phone:</label>
          <input type="text" name="mobilePhone" />
          
          <label> Names of Children:</label>
          <input id="namesOfChildren" type="text" name="children" />
          <label>Message:</label>
          <textarea name="messageContent"></textarea>
          
          <input id="submit" type="submit" name="submitButton" value="Send" />
          <? echo $msg ?>
        </form>
  </body>

</html>

 

Now I am just seeing this at the top of the screen everytime I refresh the form, but it wont let me send the form:

 

new contact!

" . $nm . "
" . $em . "
" . $hp . "
" . $mp . "
" . $ch . "
" . $mc; $mail->isHTML(true); //send an email return $mail->send(); } $name = $_POST['name']; $homephone = $_POST['homePhone']; $email = $_POST['email']; $mobilephone = $_POST['mobilePhone']; $children = $_POST['children']; $messagecontent = $_POST['messageContent']; return sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent)); if (sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent)){ $msg = "Mail was sent successfully"; else $msg = "Woops, something went wrong. Please Try Again"; }; } ?>

 

I'm wondering if anybody could help me. If you know any videos online that are helpful, I would appreciate it.

Share this post


Link to post
Share on other sites

It means you don't have PHP set up correctly. It must be installed appropriately for whatever web server software you're using (such as Apache or nginx), the file needs to be named with the .php extension, and your browser has to say "http://" in the address bar (unless you're using Chrome, which won't show you that part even though it is there).

Share this post


Link to post
Share on other sites

Thank you for your response, I have PHP downloaded on my computer, and I downloaded XAMPP, turned it on, and enabled the network and services (Apache included). I tested it out on a website I already have up, but now it just shows all of my e-mail info in the website (which I immediately changed). The submit button no longer sends me to an error page, but it didn't do anything.

 

Does this mean I have to inquire with the hosting service I am using to see how I can fix this?

Share this post


Link to post
Share on other sites

You probably don't need to talk to your hoster. Not yet.

 

What do you mean it just "shows all your email info"? If the submit button doesn't send you to an error page, what do you end up at?

Share this post


Link to post
Share on other sites
Posted (edited)

What I meant by that is that I used $mail->Username and $mail->Password to put in my e-mail and password. And all that info just popped up at the top of my website. I fixed it though, so it doesn't do that anymore. After changing the code some more, the form seems to be functioning, but now it only gives me the "Woops, something went wrong. Please Try Again" validation everytime I try sending it:

<?php
    if (isset($_POST['submitButton'])) {
        
        require 'phpmailer/PHPMailerAutoload.php';

        function sendemail($nm, $hp, $em, $mp, $ch, $mc){
            //create an instance of PHP Mailer
            $mail = new PHPMailer();
            $mail->host = "stmp.gmail.com";
            //enable STMP
            //$mail->isSTMP();
            $mail->STMPAuth = true;
            $mail->Username = "redacted";
            $mail->Password = "redacted";
            $mail->STMPSecure = "ssl"; //or we can use TLS
            $mail->Port = 465; //or 587 if TLS

            //set content of e-mail
            $mail->setFrom($em, $nm);
            $mail->addAddress('redacted', 'Name of the Administrator');
            $mail->Subject = 'New Contact';
            $mail->Body = "You've received a new contact! <br /></br />" . $nm . "<br />" . $em . "<br />" . $hp . "<br />" . $mp . "<br />" . $ch . "<br />" . $mc;
            $mail->isHTML(true);

            $mail->send();
            
        }
        
        $name = $_POST['name'];
        $homephone = $_POST['homePhone'];
        $email = $_POST['email'];
        $mobilephone = $_POST['mobilePhone'];
        $children = $_POST['children'];
        $messagecontent = $_POST['messageContent'];

        

        if (empty($name) || empty($homephone) || empty($email) || empty($mobilephone) || empty($children) || empty($messagecontent)){
            $msg = "Woops, something went wrong. Please Try Again";
        }
        else{
            $msg = "Mail was sent successfully";
            return sendemail($name, $homephone, $email, $mobilephone, $children, $messagecontent);
        };
    }
Edited by cyberRobot
please use [code][/code] tags

Share this post


Link to post
Share on other sites

...but now it only gives me the "Woops, something went wrong. Please Try Again" validation everytime I try sending it:

 

Did you check all your form variables to make they are not empty? You could start by displaying the $_POST variable.

<?php
//...
$children = $_POST['children'];
$messagecontent = $_POST['messageContent'];
 
 
print '<pre>' . print_r($_POST, true) . '</pre>';
 
 
if (empty($name) || empty($homephone) || empty($email) || empty($mobilephone) || empty($children) || empty($messagecontent)){
//...
?>

If the POST array looks good, check the individual variables ($name, $homephone, etc.) to make sure the values were assigned correctly. For example:

$messagecontent = $_POST['messageContent'];
 
var_dump($messagecontent);

Share this post


Link to post
Share on other sites

Side note: the <label> tags are set up incorrectly. They need to surround both the label and the input field.

<label>Name: <input type="text" name="name" /></label>
 
...or you can use the <label> tag's "for" attribute and the input field's "id" attribute to establish a partnership. More information can be found here:
 
There's an easy way to tell if the <label> tag is working. Clicking the field label should place the cursor in the corresponding form field.

Share this post


Link to post
Share on other sites

Hello,

Thank you for your feedback. I checked to make sure that all the Variables were not empty. The form started working when I put values in the form, other than just zeroes. I then used Javascript so that the values will be displayed and sent in a way that will work best for the form (i.e. putting the dashes in the phone number). After working on it more I got it to finally work. Thank you!

 

Also thank you for letting me know about the label tags.

Share this post


Link to post
Share on other sites

The form started working when I put values in the form, other than just zeroes.

 

Have you considered using something other than the empty() function when testing the values? You will want to try something different for the number of children since "0" should be an acceptable answer.

 

For example, you could do something like this:

if ($name=='' || $homephone=='' || $email=='' || $mobilephone=='' || $children=='' || $messagecontent==''){

Better yet, you could generate a list of errors based on the fields left blank. If there are errors, re-display the form (populated with the user's previous responses) and let the user know which fields are missing.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.