Вопрос

I am using PHP to send data to an email address from a HTML form. It worked fine while the PHP file was a pure PHP file, displaying the confirmation text upon submitting the form. However, I needed the confirmation text to appear within our usual templates so I added the same PHP into the body of a page and set the form action to go to that page. When someone now submits the form, an email does get sent but it contains none of the information from the form. Can you help?

HTML:

<form method="post" action="thank-you-page.html"> 
 Email: <input name="email" type="text"><br />
Name: <input name="name" type="text"><br />

<h3>Your message</h3> 
Subject: <input name="subject" type="text"><br />
 Message:<br /> <textarea name="message" rows="15" cols="40"></textarea><br /> 
 <input type="submit" /> 
 </form>

PHP within body of thank-you-page.html:

<?php 
 $to = "myemail@email.com"; 

 $subject = 'Feedback from online form'; 
 $email = $_REQUEST['email'] ; 
 $message = $_REQUEST['message'] ; 
 $headers = "From: $email"; 
 $sent = mail($to, $subject, $message, $headers) ; 
 if($sent) 
 {print 'Your mail was sent successfully.  Thank you for your feedback.'; }
 else 
 {print 'We encountered an error sending your mail.'; }
 ?> 

Thank you!

Это было полезно?

Решение

Your thank you page needs to be a PHP page, not just an HTML page.

Change it to be thank-you-page.php

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top