I’ve no experience with php, due to an old formtomail cgi script no longer working on a host server I decided to switch to a simple html to php mail for a contact page on a website. I took a template online and set up the html page but having difficulty editing the mail.php file so that all the data requests on my html form get emailed over to me. The template I use just had email and message. My html contact page has different requests i.e contact name not name, and asks for more information. so I need the php form to email all this information and need help on how to edit the php file to include whats requested on the html contact page.
I have a contact form HTML page with the following:
<form action="mail.php" method="POST">
<p class="bodytextgrey">
Contact Name:<br /><input type="text" name="contact name" size="50" /><br />
Firm Name:<br /><input type="test" name="firm name" /><br /><br />
E-mail: (Please enter the email address you would like the document is to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br /><input type="test" name="job number" /><br /><br />
Document you require:<br /><form action=""><select name="Document you require">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br /><input type="text" name="Discount code" size="20" /><br /><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
I then have a mail.php with this (MY EMAIL is my actual email):
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name n Message: $message";
$recipient = "MY EMAIL";
$subject = "Contact Form";
$mailheader = "From: $email rn";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Would also like it to bring up an existing thank you.html page rather than just a thankyou word on a white page so also need help adding in how I link this.
Any help would be appreciated
2
Answers
You can use the header() function:
Example:
Formatting your code will help immensely to start debugging this. I’ve reformatted your current HTML with some indenting and line breaks to help make it clear:
There are a few things wrong with this:
<p>
with no</p>
(closing tag) anywhere<form action="">
nested inside of an existing form isn’t allowed and will cause submission issues (see "Permitted content" at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form)</form>
that exists matches the<form action="">
, but you’re missing a</form>
message
field anywhereHere’s the updated HTML with my suggested fixes:
As others have mentioned, it’s not clear what the 500 error is, so that will need to be resolved before this will work. But here’s a cleaned up version of the PHP. This has been simply tested at https://3v4l.org/WePRh, though it won’t show the form or any input values, but it does prove that the script runs. This is formatted better, includes all form inputs, and should make things easier to work with: