HI, Trying to complete subscription page. When I click send button, it transfers to blank page. Know something’s wrong, but unsure. Any help appreciated.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
The form attributes method and action. The method specifies which method is used to send the data to the server and the action is the URL of the server file that processes the sent data. I’ll leave it to you to investigate what other methods are and what’s the default one if the attribute is not specified.
For the input elements, each one needs to have a name, otherwise PHP won’t recognize the data.
The button with a type of submit, which is in charge of actually sending the form to the server.
The sender.php receives the data after we click the submit button.
<?php
// sender.php
if (
isset($_POST['sender_name'])
&& isset($_POST['sender_mail'])
&& isset($_POST['sender_message'])
) { // Check if any of the fields is missing, otherwise send the mail
$name = $_POST['sender_name'];
$mail = htmlspecialchars($_POST['sender_mail']);
$message = $_POST['sender_message'];
$headers = array(
'From' => 'myweb@mydomain.com', // Replace this with an email using Your domain
'Reply-to' => $mail
);
if (mail('keith.williams@bloggoneit.net', "You have a subscription from $name <$mail>", $message, $headers)) {
// The message was sent.
echo "<p>Message sent</p>";
} else {
// Something went wrong and You'll need to look into the error logs
echo "<p style='color: red'>We failed to send the message :(</p>";
} else {
// If this message appears, then one or more fields were empty
echo "<p style='color: red'>Please fill the required information</p>";
}
I must warn You though, this is not useful for production, only for learning purposes.