Home > Software design >  Prevent PHP POST method from redirecting
Prevent PHP POST method from redirecting

Time:06-13

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);
?>

CodePudding user response:

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

CodePudding user response:

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);
    }
  •  Tags:  
  • html
  • Related