0

I'm aware there are other posts on this topic but none of the few solutions provided have worked for me.

I'm using an HTML form to execute some PHP code to send an email. On clicking the submit button though I'm redirected to a new page and I need to prevent this.

Here's my HTML code:

<form method="post" name="myemailform" action="send-email.php">
          <input type="text" required placeholder="Name" size="19" name="name" style="width: 15vw"><br><br>
          <input type="email" required placeholder="Email" name="email" style="width: 15vw"><br><br>
          <textarea type="text" required placeholder="Message" name="message" style="width: 25vw; height: 15vh; resize: none"></textarea>
          <br><br>
          <button onclick="change()" type="submit" value="Submit" id="send_button">SEND</button>
      </form>

And my PHP code:

<?php
    $name = $POST['name'];
    $visitor_email = $POST['email'];
    $message = $POST['message'];
    $email_from = 'my email';

    $email_subject = "***Email from Personal Website";

    $email_body = "You have received a new message from $name.\n".
                            "Here is the message:\n $message \n".

    $to = "my email";

    $headers = "From: $email_from \r\n";

    $headers .= "Reply-To: $visitor_email \r\n";

    mail($to,$email_subject,$email_body,$headers);
?>
  • The button tag onclick="change()" is there a reason for this? Otherwise i would remove it. And test – MisterG13 Jun 12 '22 at 18:32
  • @MisterG13 It's just to change the color and text of the button on click. I'll try but not really sure why it would make a difference. Edit: no difference. – Matthew Kaplan Jun 12 '22 at 18:58

2 Answers2

1

try :

<form method="post" name="myemailform" action="send-email.php" target="_top" >

to force it to the top, though I'm not sure why its making a new tab to begin with, since I copied the code to my test server and as-is it doesn't with firefox.

but,

$POST needs to be $_POST
tangledtrees
  • 121
  • 5
  • I misworded the OP, there wasn't a new tab being opened but rather I was being redirected. For example, from myurl.com to myurl.com/send-email.php. Is there any way to run the PHP in place without that redirection to a blank page? – Matthew Kaplan Jun 12 '22 at 19:10
  • yes, you can submit to yourself, but the PHP and the html then would need to be in the same file. I usually generate the HTML with the PHP in such a case, but changing your code as little as possible, I'll post the edited file as another answer. – tangledtrees Jun 12 '22 at 19:17
0

Again, changing your code as little as possible and doing the simplest check (seeing if there is something in _POST), the below works, but will always display the form -- not sure if that's desired or not.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" name="myemailform" action="" target="_top" >
    <input type="text" required placeholder="Name" size="19" name="name" style="width: 15vw"><br><br>
    <input type="email" required placeholder="Email" name="email" style="width: 15vw"><br><br>
    <textarea type="text" required placeholder="Message" name="message" style="width: 25vw; height: 15vh; resize: none"></textarea>
    <br><br>
    <button onclick="change()" type="submit" value="Submit" id="send_button">SEND</button>
</form>

</body>
</html>

<?php
if (!empty($_POST))
    {
    echo ( "sending email php<br/>") ;
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];
    $email_from = 'my email';

    $email_subject = "***Email from Personal Website";

    $email_body = "You have received a new message from $name.\n".
        "Here is the message:\n $message \n".

        $to = "my email";

    $headers = "From: $email_from \r\n";

    $headers .= "Reply-To: $visitor_email \r\n";

    mail($to,$email_subject,$email_body,$headers);
    }
tangledtrees
  • 121
  • 5
  • This works well, thank you so much! The only small issue I'm having is on submission it sends you back to the top of the page. Is there any way to remain in place? – Matthew Kaplan Jun 12 '22 at 19:42
  • It is reloading the page, so it starts at the top. You could add some javacript + html to scroll to a specific location. That's really an HTML question -- this might answer it: https://stackoverflow.com/questions/24739126/scroll-to-a-specific-element-using-html – tangledtrees Jun 12 '22 at 19:52