Jump to content

Form Info being sent to Email


msugi002

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.

Link to comment
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).

Link to comment
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?

Link to comment
Share on other sites

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);
        };
    }
Link to comment
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);
Link to comment
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.
Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.