Problem with From field in contact form and mail() function

Posted by Matthew on Stack Overflow See other posts from Stack Overflow or by Matthew
Published on 2010-12-28T18:30:44Z Indexed on 2011/01/17 19:53 UTC
Read the original article Hit count: 198

Filed under:
|
|
|
|

I've got a contact form with 3 fields and a textarea...
I use jQuery to validate it and then php to send emails.

This contact form works fine but, when I receive an email, From field isn't correct. I'd like to want that From field shows text typed in the Name field of the contact form. Now I get a From field like this: <[email protected]>

For example, if an user types "Matthew" in the name field, I'd like to want that this word "Matthew" appears in the From field.

This is my code (XHTML, jQuery, PHP):

<div id="contact">
    <h3 id="formHeader">Send Us a Message!</h3>
    <form id="contactForm" method="post" action="">
        <div id="risposta"></div> <!-- End Risposta Div -->
        <span>Name:</span>
        <input type="text" id="formName" value="" /><br />
        <span>E-mail:</span>
        <input type="text" id="formEmail" value="" /><br />
        <span>Subject:</span>
        <input type="text" id="formSubject" value="" /><br />
        <span>Message:</span>
        <textarea id="formMessage" rows="9" cols="20"></textarea><br />
        <input type="submit" id="formSend" value="Send" />
    </form>
</div>


<script type="text/javascript">
     $(document).ready(function(){
        $("#formSend").click(function(){

            var valid = '';
            var nome = $("#formName").val();
            var mail = $("#formEmail").val();
            var oggetto = $("#formSubject").val();
            var messaggio = $("#formMessage").val();

            if (nome.length<1) {
                valid += '<span>Name field empty.</span><br />';
            }
            if (!mail.match(/^([a-z0-9._-][email protected][a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
                valid += '<span>Email not valid or empty field.</span><br />';
            }
            if (oggetto.length<1) {
                valid += '<span>Subject field empty.</span><br />';
            }

            if (valid!='') {
                $("#risposta").fadeIn("slow");
                $("#risposta").html("<span><b>Error:</b></span><br />"+valid);
                $("#risposta").css("background-color","#ffc0c0");
            }
            else {
                var datastr ='nome=' + nome + '&mail=' + mail + '&oggetto=' + oggetto + '&messaggio=' + encodeURIComponent(messaggio);
                $("#risposta").css("display", "block");
                $("#risposta").css("background-color","#FFFFA0");
                $("#risposta").html("<span>Sending message...</span>");
                $("#risposta").fadeIn("slow");
                setTimeout("send('"+datastr+"')",2000);
            }
            return false;
        });
    });
    function send(datastr){
        $.ajax({    
            type: "POST",
            url: "contactForm.php",
            data: datastr,
            cache: false,
            success: function(html) {
            $("#risposta").fadeIn("slow");
            $("#risposta").html('<span>Message successfully sent.</span>');
            $("#risposta").css("background-color","#e1ffc0");
            setTimeout('$("#risposta").fadeOut("slow")',2000);
            }
        });
    }
    </script>

<?php
$mail = $_POST['mail'];
$nome = $_POST['nome'];
$oggetto = $_POST['oggetto'];
$text = $_POST['messaggio'];
$ip = $_SERVER['REMOTE_ADDR'];
$to = "[email protected]";

$message = $text."<br /><br />IP: ".$ip."<br />";
$headers = "From: $nome \n";
$headers .= "Reply-To: $mail \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=UTF-8 \n";

mail($to, $oggetto, $message, $headers);
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery