Saturday 25 August 2018

php - Variable not working in mail form?

I have a HTML form with 4 id (name, email, message, subject), a js with all the variables declared and a PHP that should send the mail.



HTML



            












This is the js



    var valid = '';

var isr = ' requested.';
var name = $("#nome").val();
var mail = $("#email").val();
var subject = $("#subject").val();
var messaggio = $("#messaggio").val();


(follow controls about the name and mail, and the send function)



This is the php




    $mail = trim($_POST['mail']);
$name = $_POST['name'];
$text = $_POST['messaggio'];
$subject = $_POST['subject'];
$ip = $_SERVER['REMOTE_ADDR'];

$to = "admin@test.com";

$message = "Username: ".$name.", ".$mail.".
";

$message .= "Subject: ".$subject.".
";
$message .= "Messaggio:
".$text."

";
$message .= "IP: ".$ip."
";
$headers = "From: ".$mail." \r\n";
$headers .= "Reply-To: ".$mail." \r\n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";

if(mail($to, $subject, $message, $headers)){
echo "
Message sent!
";

}


I posted only the relevant code. When I click send I'll receive the mail, however the field "subject" is blank, as if the variables "subject" had been ignored.



Could you please help me? I'm starting to learn PHP but I'm still a newbie. Thank you.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...