i'm setting up a contact form in functions.php My problem is that when ever i refresh the page, the script sends a mail. This should only happen when someone submits the form. I would appreciate if someone could help
function myform(){
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$navn = sanitize_text_field($_POST['navn']);
if(!isset($error)){
$to = 'test@email.com';
$subject = 'test';
$body = '<h1>My text</h1>' . '<p>' . $navn . '</p>';
$headers = array('Content-Type: text/html; charset=UTF-8', 'Cc: test2@email.com');
if(wp_mail( $to, $subject, $body, $headers )){
wp_redirect( 'http://localhost:8888/test/takk/' ); exit;
}
}
}
}
add_action('init', 'myform');