私はかなり基本的なメールフォームを持っています
<form name="contactform" method="post" action="send_email.php" id="email_form">
<div class="ContactHeaders">Name</div>
<input type="text" name="Name" class="ContactBoxes" id="name"/><br/><br/>
<div class="ContactHeaders">Email</div>
<input type="email" name="Email" class="ContactBoxes"/><br/><br/>
<div class="ContactHeaders">Message</div>
<div style="width:100%">
<textarea name="Message" maxlength="1000"></textarea><br/>
</div>
<div style="width: 100%">
<input type="submit" class="Submitbtn" value="Submit">
</div>
</form>
ここに「send_email.php」があります
<?php
ob_start();
include 'navbar.php';
ob_end_clean();
if(isset($_POST['Email'])) {
//declare variables
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$complete = 0;
$email_to = "someone@example.com";
$email_subject = "Website Contact";
//check all forms are filled in correctly
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(preg_match($email_exp,$Email)) {
$complete++;
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(preg_match($string_exp,$Name)) {
$complete++;
}
if(strlen($Message) > 20) {
$complete++;
}
//send email if $complete = 4
if ($complete == 3){
if (get_magic_quotes_gpc()) {
$Message = stripslashes($Message);
}
$Message = $Message . "\n\n" . "From " . $Name;
$headers = 'From: '.$Email."\r\n".
'Reply-To: '.$Email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $Message, $headers);
echo '<script>
alert("Thank you for contacting me. I will be in touch shortly");
top.location="index.php";
</script>';
}
else {
echo '<script>
alert("The form you submitted is invalid. Please ensure the following: \n You have provided a valid email address. \n Your phone number is entered correctly. \n The message box contains at least 20 characters.")
history.go(-1)
</script>';
}
}
?>
したがって、私の問題は、フォームがphpファイルに送信されると、ブラウザーがphpファイルをロードし、コードを実行して結果を返すことです(実際には問題ではありませんが、私は望んでいません)アラートを表示し、1つ戻ります(とにかく私のコードで)。バックグラウンドでコードを実行する方法はありますか?フォームは、(それが理にかなっている場合) 行く必要なく php ファイルを実行し、同じウィンドウに結果を返します。私は明らかにAJAXについていろいろ調べて見つけましたが、私はそれを本当に理解していなかったし、それを私のために働かせることができませんでした.
これを行う理由は少し複雑ですが、私のサイトの使いやすさと見栄えが良くなる限り、物事ははるかに簡単になります (空白のページに移動してアラートを表示するのはあまり良くありません)。
あなたが私に提供できる助けを前もって感謝します:)