Hi all =) newbie here...just trying to figure out PHP for the first time so I can add a Contact Form to my website....
I have been following an article written by my web hosting provider on how to create a basic PHP to email script (Link Here) which is working fine when I copy and paste the code, but when I try to add my own elements (a single checkbox for example), it seems to break the code and gives me 500 errors. I managed to add a single HTML Text Input which emails me the results as required (see $messagesubject in the PHP below), but when I add a checkbox ($sub), I cannot figure out what code I need to use :/
The basic concept of the form is Name, Email, Subject and Message. Then I want to add a small checkbox which if enabled, will let me know via email that the user wants to sign-up for newsletters, offers etc....Initially, I added the "checked" value to the HTML form to set it be default - which works fine - but when the checkbox is manually de-selected, it throws a 500 error at me when the form is submitted?
<input type="checkbox" name="sub" checked>
This is the HTML I used (above). And the PHP (below)
<?php
$email_to = "name@domain.co.uk";
$name = $_POST["name"];
$email_from = "contact@domain.co.uk";
$reply_to = $_POST["email"];
$messagesubject = $_POST["messagesubject"];
$message = $_POST["message"];
$sub = $_POST["sub"];
$email_subject = "Message received from Your Site";
$headers = "From: " . $email_from . "\n";
$headers .= "Reply-To: " . $reply_to . "\n";
$message = "Subbed: ". $sub . "\r\nSubject: ". $messagesubject . "\r\nName: ". $name . "\r\nMessage: " . $message;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{
header("Location: http://www.yourdomain.co.uk/thankyou.html");
} else {
echo "There has been an error sending your comments. Please try later.";
}
?>
When I added the $sub, it works fine when set as default. The email outputs the "Subbed" as "On". But if the user de-selects it, the 500 error appears. Any help with this would be massively appreciated and sorry if it's such a basic problem I am overlooking....like I said I am fairly new to web development etc/...........Thanks, Vi =)