Diaconu Eduard
Diaconu Eduard

Reputation: 161

Return message status in page after send mail with php

I try to show a message in html page, after send an e-mail. I don't want to use javascript with an alert, just a simple message after send button.

I made a contact php page with this code:

<?php

$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_telefon = $_POST['cf_telefon'];
$field_message = $_POST['cf_message'];
$mail_to = '[email protected]';;
$from = 'Mesaj nou de la:'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Telefon: '.$field_telefon."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $from, $body_message, $headers);

 ?>

And in html page, i insert a contact form with this code:

       <form  action="contact.php" method="post">
                        <div class="form-group">
                            <label for="name">
                                Nume
                            </label>
                            <input type="text" name="cf_name" placeholder="" id="name" class="form-control" required="true">
                        </div>
                        <div class="form-group">
                            <label for="email">
                                Email
                            </label>
                            <input type="text" name="cf_email"  placeholder="" id="email" class="form-control" required="true">
                        </div>
                        <div class="form-group">
                            <label for="phone">
                                Telefon
                            </label>
                            <input type="text" name="cf_telefon" id="phone" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="phone">
                                Mesaj
                            </label>
                            <textarea name="cf_message" placeholder="" rows="5" class="form-control" required="true">
                            </textarea>
                        </div>

                        <input class="btn btn-info" type="submit" value="Trimite" >
<?php if($send_mail) {
if($mail_status){
print "succes";
exit();
}
else {
print "eroare";
}
        }       
?>
                    </form>

Email was successfully sent, but the message isn't show. After press send button, contact form return a blank page. I want to return the same page with message. (ater refresh)

Maybe exist another way to do that? With get or something like that?

Thanks for help!

Upvotes: 1

Views: 2056

Answers (2)

Aman Maurya
Aman Maurya

Reputation: 1325

    if($_POST){
      // send mail code here
      $field_name = $_POST['cf_name'];
      $field_email = $_POST['cf_email'];
      $field_telefon = $_POST['cf_telefon'];
      $field_message = $_POST['cf_message'];
      $mail_to = '[email protected]';;
      $from = 'Mesaj nou de la:'.$field_name;
      $body_message = 'From: '.$field_name."\n";
      $body_message .= 'E-mail: '.$field_email."\n";
      $body_message .= 'Telefon: '.$field_telefon."\n";
      $body_message .= 'Message: '.$field_message;
      $headers = 'From: '.$field_email."\r\n";
      $headers .= 'Reply-To: '.$field_email."\r\n";
      $mail_status = mail($mail_to, $from, $body_message, $headers);
      if($mail_status){
       echo 'mail send';
      }else{
       echo 'mail not send';
      }
    }

    //place html code here below the php code
           <form  action="" method="post">
                        <div class="form-group">
                            <label for="name">
                                Nume
                            </label>
                            <input type="text" name="cf_name" placeholder="" id="name" class="form-control" required="true">
                        </div>
                        <div class="form-group">
                            <label for="email">
                                Email
                            </label>
                            <input type="text" name="cf_email"  placeholder="" id="email" class="form-control" required="true">
                        </div>
                        <div class="form-group">
                            <label for="phone">
                                Telefon
                            </label>
                            <input type="text" name="cf_telefon" id="phone" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="phone">
                                Mesaj
                            </label>
                            <textarea name="cf_message" placeholder="" rows="5" class="form-control" required="true">
                            </textarea>
                        </div>

                        <input class="btn btn-info" type="submit" value="Trimite" >

                    </form>

Upvotes: 1

Option
Option

Reputation: 2645

Place your PHP in the top of the page of where your contact form is.

then change: <form action="contact.php" method="post">

to

<form action="" method="post">

you also need to add name="submit" to your submit button field and then wrap:

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

// code

}

Also if you are trying to make sure people fill in all fields, you should use

if (!empty($var))
{
// code
} else
echo "Fill in this field please";
}

required fields can be easily bypassed by just using required within the html side.

EDIT: once all conditions are met you can simply add echo "thanks" or whatever message you want to beneath the mail() field for a success message which will output to the page above the form.

Upvotes: 1

Related Questions