user2584238
user2584238

Reputation: 33

I want to echo message when form is submitted

I'm not very good at PHP (more of a front-end developer) and I'm doing a website for a client. I got everything working up to now except for one thing. When the user fills out the contact form and submits it successfully i want to display a message that tells them the email was sent. I already have the div set up I just don't know who to execute it with PHP.

<div class="sent">Email has been sent.</div>
    <form action="" method="POST" class="contact-form"> 
    <img class="close" src="images/close-icon.png">     
        <div class="top-title">Contact Us</div>
        <input type="hidden" name="action" value="submit"> 
        <div class="input-title">Name*</div> 
        <input class="input" name="name" type="text" value="" size="30" required><br>
        <div class="input-title">Phone Number*</div> 
        <input class="input" name="phone" type="text" value="" size="30" required><br>
        <div class="input-title">Email Address*</div>
        <input class="input" name="email" type="text" value="" size="30" required><br> 
        <div class="input-title">How did you hear about us?</div>  
        <input class="input" name="company" type="text" value="" size="30"/><br> 
        <div class="input-title">Let us know how we can help you</div>
        <textarea class="textarea" name="message" rows="7" cols="30"></textarea><br> 
        <div class="required">*Fill is required</div>
        <input class="submit-button" type="submit" value="Send Email"/> 
    </form> 
    <?php              
        { 
        $name=$_POST['name']; 
        $email=$_POST['email']; 
        $phone=$_POST['phone']; 
        $company=$_POST['company']; 
        $message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];

            $from="From: $name<$email>\r\nReturn-path: $email"; 
            $subject="GS Website Message"; 
            mail("[email protected]", $subject, $message, $from); 
        }
    ?> 

Upvotes: 0

Views: 2611

Answers (2)

Vasyl Lukanych
Vasyl Lukanych

Reputation: 3

$password=md5($_POST ['password']);

$password3 =md5($_POST ['password2']);
if($password != $password3)
{
   $message = "Passwords do not match";

}

etc.

you need add 

<form>
<div class="form-group">
                                            <input type="password" name="password"  id="password" class="form-control" placeholder="Password *" value="" required/>
                                        </div>
                                        <div class="form-group">
                                            <div class="message"><?php if(!empty($message)) { echo $message; } ?></div>
                                            <input type="password" name="password2" class="form-control" id="password2" placeholder="Confirm Password *" value="" required/>
                                        </div>
</form>

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Change: (and using a conditional statement)

mail("[email protected]", $subject, $message, $from);

to:

if(mail("[email protected]", $subject, $message, $from)){
   $sent = "Message sent";
}

Then modify:

<div class="sent">Email has been sent.</div>

to read as:

<div class="sent"><?php if(!empty($sent)) { echo $sent; } ?></div>

Seeing you're new to PHP/forms, it's best to also check if your inputs are empty or not, otherwise you risk in either not getting mail, or receiving empty data.

Set inside a conditional statement also.

I.e.: using NOT empty: ! is the NOT operator in PHP.

if(!empty($_POST['var'])){...}

Edit:

The logic needs to be reversed.

First, place your PHP above your HTML form.

I also added a submit name attribute to your submit button, with a conditional statement and a ternary operator in the div.

<?php

 if(isset($_POST['submit'])){

        $name=$_POST['name']; 
        $email=$_POST['email']; 
        $phone=$_POST['phone']; 
        $company=$_POST['company']; 
        $message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];

            $from="From: $name<$email>\r\nReturn-path: $email"; 
            $subject="GS Website Message"; 


if(mail("[email protected]", $subject, $message, $from)){

$sent = "Message sent";

    }
}
    ?> 

<div class="sent"><?php echo isset($sent) ? $sent : ''; ?></div>


    <form action="" method="POST" class="contact-form"> 

    <img class="close" src="images/close-icon.png">     


        <div class="top-title">Contact Us</div>
        <input type="hidden" name="action" value="submit"> 
        <div class="input-title">Name*</div> 
        <input class="input" name="name" type="text" value="" size="30" required><br>
        <div class="input-title">Phone Number*</div> 
        <input class="input" name="phone" type="text" value="" size="30" required><br>
        <div class="input-title">Email Address*</div>
        <input class="input" name="email" type="text" value="" size="30" required><br> 
        <div class="input-title">How did you hear about us?</div>  
        <input class="input" name="company" type="text" value="" size="30"/><br> 
        <div class="input-title">Let us know how we can help you</div>
        <textarea class="textarea" name="message" rows="7" cols="30"></textarea><br> 
        <div class="required">*Fill is required</div>
        <input class="submit-button" type="submit" value="Send Email" name="submit"/> 
    </form> 

Upvotes: 1

Related Questions