Subscription php

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.

<?php
if(isset(_POST['submit'])){
$mailto = "keith.williams@bloggoneit.net"; 
$name = htmlspecialchars($_POST['name']); 
$mailfrom = htmlspecialchars($_POST['mail'];
$headers = "From:" . $mailfrom; 
$txt = "You have a subscription from".$name;


     mail($mailfrom, $mailto, $headers, $txt);   
 

      print"<b>Email successfully sent</b>";
 }    
?>

could u post your code with the single “”" bellow and above?
its really hard to read now

1 Like
<?php
if(isset(_POST[‘submit’])){
$mailto = "keith.williams@bloggoneit.net";
$name = htmlspecialchars($_POST[‘name’]);
$mailfrom = htmlspecialchars($_POST[‘mail’];
$headers = “From:” . $mailfrom;
$txt = “You have a subscription from”.$name;

 mail($mailfrom, $mailto, $headers, $txt);   


  print"<b>Email successfully sent</b>";

}
?>

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.

Note: Backticks are not single quotes.

markdown_Forums

<p><?php
if(isset(_POST[‘submit’])){
$mailto = "keith.williams@bloggoneit.net";
$name = htmlspecialchars($_POST[‘name’]);
$mailfrom = htmlspecialchars($_POST[‘mail’];
$headers = “From:” . $mailfrom;
$txt = “You have a subscription from”.$name;

 mail($mailfrom, $mailto, $headers, $txt);   


  print"<b>Email successfully sent</b>";

}
?></p>

Thank you

Hello and Welcome!

You have some syntax errors.

  • The array that contains the data from the form is called $_POST, not _POST (You’re missing a $).
  • You’re using the wrong type of single quote. The right one is ', but You’re using .
  • You’re using the wrong type of double quotes too. The right one is ", but You’re using `“.

Other than that seems to be OK.

1 Like

Corrected the errors, when I click send, it still transfers to a blank screen.

Are You certain that the script receives the data? Is there a form element with an attribute name that equals input?

The form is subscription.php and when I send it, it just appears blank.

Print the array before the if to see what you get:

print_r($_POST);

By the way, your form is using the post method to send the data and not the default?

deleted an old file(subscriptionform.php), replaced it with subscription.php, but it’s still appearing on server.

Are You a programmer or have any knowledge about development? I’m not trying to offend You in any way, just trying to know how much You do know.

I know little, and am learning, but still a newbie.

There are two common ways to do what You’re trying: use separate files or a single one. I’ll use the first because it’s more easy to maintain.

You will need two files: form.html and sender.php. Both files need to be on the same directory (until you learn more at least).

On the form.html you write the HTML required to create your contact form:

<form id="contact-me" method="post" action="sender.php">
  <input type="text" name="sender_name" placeholder="Your name" />
  <input type="text" name="sender_mail" placeholder="Your Email" />
  <textarea name="sender_message" placeholder="Your message"></textarea>
  <button type="submit">Send</button>
</form>

The important parts of this are:

  • 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.