0

I'm trying to create a PHP form that allows users to insert their email address and it automatically sends me an email with their email address. something like a subscription.

what I have so far is this:

<form  action="" method="post">
    <input type="email" name="email" placeholder="Enter your email address" /><br>
</form>

I found this PHP sample that I believe answers my problem, but I have no idea how to call it from my HTML.

<?php
if(isset($_POST['email'])){
$email = $_POST['email'];
$to = 'myemail@something.com';
$subject = 'new subscriber';
$body = '<html> 
            <body>
                <p>Email:<br>'.$email.'</p>
            </body>
        </html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset-utf-8";

$send = mail($to, $subject, $body, $headers);
if($send){
    echo '<br>';
    echo 'thanks';
}else{
    echo 'error';
}
}
?>
xicocana
  • 115
  • 2
  • 9
  • Just put the php file name in the action of the form – Lelio Faieta May 25 '18 at 17:30
  • Finding code on the internet can be helpful, but not if you don't have the requisite knowledge of what that code *does* or *how to use it*. Start with some basic introductory tutorials on PHP. Once you have PHP working at all, move on to some tutorials and examples of how to send email with PHP, such as the one you have here. – David May 25 '18 at 17:32
  • @LelioFaieta i tried that.. when i press submit it open index/phpfilename.php with the code printed on screen.. – xicocana May 25 '18 at 17:39
  • Do you have any other code besides the form part you created. Like what is your submit button called etc. If I can see what else you have for your form than I can help out more. But, right now I can't. Share more code please and I would be glad to help. – Jonathan Vasiliou May 25 '18 at 17:45
  • @xicocana: *"when i press submit it open index/phpfilename.php with the code printed on screen"* - This should help: https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page Basically it sounds like PHP isn't installed or configured properly on your web server. – David May 25 '18 at 17:59
  • @xicocana I was going to same the same thing David said because I created it with my server and everything went smoothly. – Jonathan Vasiliou May 25 '18 at 18:07

1 Answers1

1

There's insufficient code for me to be able to answer completely, but the one thing that comes immediately to my mind is not leaving action="" empty. Try $_SERVER['PHP_SELF'] variable, it should print the path to the file that is currently running so you'll be presented with the same page, but with data in $_POST you'll send. You can try it like this:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="email" name="email" placeholder="Enter your email address" /><br>
</form>

If you wish to send data to the same file like this, please make sure your PHP code is in the same file as the HTML structure of your form. It may make things easier if you put your PHP code first, so you can exit; from the file (not displaying the form anymore) telling the user that the message has been sent or that the error has occured.