Alert for button submission

Thank you very much for your time.

This is my PHP which I called “mailcontadinobio.php” instead of “submit.php”.

<?php
/* Set e-mail recipient */
$myemail = "mail@ilcontadinobio.it";
$subject = "New e-mail address from ilcontadinobio.it";
$emailErr = "";

/* If e-mail is not valid show error message */
if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = $_POST["email"];
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
    }
  }
/* Let's prepare the message for the e-mail */
$message = "E-mail: $email";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: index.html');
exit();

function show_error( $emailErr)
{
?>
<html>
<body>

<strong><?php echo $emailErr; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

It was a more complex piece of coding; I stripped it almost to the essential. You can spot header('Location: index.html'); which gets the page reloaded when you hit the button.

Tell me if that is ok, otherwise I will strip it further making it looks like the one you suggested.

This is the original code I wanted to use

<?php
/* Set e-mail recipient */
$myemail = "YOUR EMAIL ADDRESS";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "

Name: $name
E-mail: $email
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

It’s got a twisted check_input() function I couldn’t succeed in having it work.

That said, I suppose this

$("#subscribe").on("click", function(e){
  e.preventDefault();
  var address = $("#email_address").val();
  if (!/\S+@\S+\.\S+/.test(address)){
    alert("Email address not valid!");
  } else {
    subscribeToNewsletter(address);
    $("#email_address").val("")
  }
});

go along to all my other snippets of code to either the head or at the end in the body of the page.

What about the subscribeToNewsletter() function?