我想在用php代码发送表单之前检查表单是否存在js输入错误:
[首先,我使用Javascript检查输入字段中的输入错误。如果一切正常,我希望通过Java与contactformular.submit();
一起提交联系表单然后在PHP中,我检查表单是否使用if(isset($_POST['btnSubmit']))
提交,如果要,则要执行发送代码段。当我将其上载到服务器时,Javascript似乎可以正常工作(因为在联系表单中出现红色下划线)。但是当正确填写按钮后单击该按钮时,页面会刷新,但是我没有收到电子邮件,因此我想if(isset($_POST['btnSubmit']))
无法正常工作
HTML
<form action="" method="POST" name="contactformular">
<div class="input-field">
<i class="material-icons prefix">person</i>
<input type="text" id="fullName" name="fullName" style="width: 45%">
<label for="fullName">Your Name</label>
</div>
.
.
.
<div class="input-field center">
<button name="btnSubmit" class="btn" onclick="sendEmail()" type="button">Send</button>
</div>
</form>
JS
<script>
function sendEmail() {
console.log('sending...');
var fullName = $("#fullName");
var email = $("#email");
var tel = $("#tel");
var message = $("#message");
if (isNotEmpty(fullName) && isNotEmpty(email) && isNotEmpty(tel) && isNotEmpty(message)){
contactformular.submit();
}
function isNotEmpty(caller) {
if($(caller).val() == "") {
$(caller).css("border-bottom", "1px solid red");
return false;
} else {
$(caller).css("border", "");
return true;
}
}
</script>
PHP
<?php
if(isset($_POST['btnSubmit'])){
$to = "[email protected]"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$fullName = $_POST['fullName'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$contactbyphone = $_POST['contactbyphone'];
$contactbymail = $_POST['contactbymail'];
$subject = "Anfrage von " . $_POST['fullName'];
$message = $fullName;
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
echo "Thanks for your mail";
}
?>
非常感谢您的帮助
对于您的JavaScript,我建议以下内容: