Question

I am working on my website right now and I have encountered a little problem, I am not that great at PHP but managed to put together a contact form that works fine, the code will be below, the thing is that after submitting, I want it to echo on the same page as the button, like underneath it or something like that and I don't quite know how to get there.

Some help please ?

        if ( mail($destination,$subject,$mailMessage,$mailHeader) ) 
        {
            echo "Thank You!";
        }           
        else header('Location: index.html');

    }       
    else header('Location: index.html');    

}
else header('Location: index.html');            

PS :

I think i have to change the action attribute to point to itself but I don't quite know how.

Update - Entire code:

<?php

//EMAIL VALIDATION
function validateEmail($value){
    return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $value);
}

//CHECK VARIABLES (EMPTY/NULL OR DEFAULT)
if ( isset($_POST['last']) && $_POST['last']!="Nume complet" && isset($_POST['email']) && $_POST['email']!="Email" && isset($_POST['message']) && $_POST['message']!="Recenzie/Comentariu" ) {

    //CHECK EMAIL   
    if ( validateEmail($_POST['email']) ) {



        ////////////////////// EDIT HERE  /////////////////////////

        //SET HERE YOUR DESTINATION EMAIL
        //IT SHOULD BE FROM THE SAME DOMAIN WHERE SITE IS HOSTED
        $destination="...@outlook.com";

        //SET HERE YOUR EMAIL SUBJECT
        $subject="Colibri";

        //MESSAGE DATA (HTML FORMATTED)
        $mailMessage.="<dt><strong>Nume complet:</strong></dt><dd>".$_POST['last']."</dd>";
        $mailMessage.="<dt><strong>E-mail:</strong></dt><dd>".$_POST['email']."</dd>";
        $mailMessage.="<dt><strong>Recenzie:</strong></dt><dd>";  
        $mailMessage.=nl2br($_POST['message'])."</dd></dl>";
        $mailMessage = utf8_decode($mailMessage);

        ////////////////////// END EDIT  /////////////////////////



        //SENDER EMAIL
        $mailFrom=$_POST['email'];

        //HEADER DATA
        $mailHeader="From:".$mailFrom."\nReply-To:".$_POST['name']."<".$mailFrom.">\n"; 
        $mailHeader=$mailHeader."X-Mailer:PHP/".phpversion()."\n"; 
        $mailHeader=$mailHeader."Mime-Version: 1.0\n"; 
        $mailHeader=$mailHeader."Content-Type: text/html";


        if ( mail($destination,$subject,$mailMessage,$mailHeader) ) 
        {
            echo "Thank You!";
        }           
        else header('Location: index.html');

    }       
    else header('Location: index.html');    //EMAIL VALIDATION ERROR

}
else header('Location: index.html');        //VARS ERROR        

?>

My HTML

<div class="form">
  <hr/>
  <form id="myForm" method="post" action="contact_colibri.php">
    <input type="text" value="Nume complet" id="last" name="last" class="fields" onFocus="if(this.value == 'Nume complet') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Nume complet';}"><br/>

    <input type="text" value="Email" id="email" name="email" class="fields" onFocus="if(this.value == 'Email') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Email';}"><br/>   

    <textarea cols="20" rows="5" id="message" name="message" class="fields" onFocus="if(this.value == 'Recenzie/Comentariu') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Recenzie/Comentariu';}">Recenzie/Comentariu</textarea><br/>

    <center><button class="button">Trimite</button></center>

    </form>

            </div>
Was it helpful?

Solution

So I found out one way of making it happen , Using iframe, the only thing you need to do is add :

<div class="form">
  <hr/>
  <form id="myForm" method="post" action="contact.php" target="myIframe">
  ...
  ...
</form>

<iframe name="myIframe" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

Hope this will come in handy for those trying to find out an answer to this same problem :)

OTHER TIPS

Use the blank action.

And after form code use this php code:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // your php code here
}
?>

You can try set the action of the form to the same php file, and then check for its parameters and print the message. Other way could be send the form trough ajax, and then using javascript to print the message.

Your form tag should be something like

<form method="POST" action="{LINK_TO_FORM_SUBMIT_PAGE}" .... >

In your above code. You can redirect back to the contact form on success.

Replace echo "Thank You!"; with

header('Location: {YOUR_CONTACT_FORM_LINK}?success=true');

Later, in your contact form page check for this success param.

if(isset($_GET['success']) && $_GET['success']) echo 'Thank You!';

Use jquery to post your form using ajax and echo the results in php:

Make sure to include jquery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">

$('form').on("submit", function(){

  var formData = $(this).serialize();

  $.ajax({
    type: "POST",
    url: "contact_colibri.php",
    data: formData,
    success: function(result){

       $('.feedback').html(result);   

    }
  });

  return false;
});

</script>

In your php file make sure you define the $message = "" variable before you try to append any string value:

$message = '';

And you are trying to use the $POST['name'] variable which does not exist and is not checked for either add the name field to your html form or just simply use the email from the form:

either add name in html form:

<input type="text" value="Name" id="name" name="name" class="fields">

or change in php:

$mailHeader="From:".$mailFrom."\nReply-To:".$_POST['email']."<".$mailFrom.">\n";

and now you don't need to set the header locations just echo the feedback:

echo 'Your email did not validate';
echo 'There was a problem sending your email, try again later...
echo 'You did not enter all te required fields';

make sure you add a html element with feedback below your form to show the feedback:

below your form submit button:

<p class="feedback"></p>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top