gpt4 book ai didi

javascript - HTML 表单无法发送到 PHP 文件

转载 作者:行者123 更新时间:2023-12-03 00:06:47 25 4
gpt4 key购买 nike

我的网站上有一个联系表单,我想将其发送到电子邮件,我将其设置为“onsubmit”,它将发送到 js 函数并检查是否填写了所有输入。然后,它继续转到我的 php 文件。但是,在我点击提交后,它显示“此页面无法正常工作”。我的 JS 函数运行得很好,只是我的 php 不行。

联系.html

  <p id="required">Required <span style="color: red;">*</span></p>
<script src="js/formvalidate.js"></script>
<form class="contact-form" name="form" action="php/contact.php" onsubmit="return formValidate()" method="post">
<label for="first_name" id="label_first" class="contact-label">First Name<span class="asterisk" id="required_fname"></span></label><br>
<input type="text" id="first_name" name="first_name" class="contact-input" placeholder="First Name"><br>
<label for="last_name" id="label_last" class="contact-label">Last Name<span class="asterisk" id="required_lname"></span></label><br>
<input type="text" id="last_name" name="last_name" class="contact-input" placeholder="Last Name"><br>
<label for="email" id="label_email" class="contact-label">Email<span class="asterisk" id="required_email"></span></label><br>
<input type="email" id="email" name="email" class="contact-input" placeholder="Email"><br>
<label for="subject" id="label_subject" class="contact-label">Subject<span class="asterisk" id="required_subject"></span></label><br>
<input type="text" id="subject" name="subject" class="contact-input" placeholder="Subject"><br>
<label for="message" id="label_message" class="contact-label">First Name<span class="asterisk" id="required_message"></span></label><br>
<textarea id="message" name="message" class="contact-message" placeholder="Type here..."></textarea><br>
<button type="submit" id="submit" name="submit" class="submit">Submit</button>
</form>

formvalidate.js

function formValidate(){
var form = document.forms["form"]
var first_name = form.elements["first_name"].value;
var last_name = form.elements["last_name"].value;
var email = form.elements["email"].value;
var subject = form.elements["subject"].value;
var message = form.elements["message"].value;
var output = " *";
var result = false;
if (first_name == "" || last_name == "" || email == "" || subject == "" || message == "") {
alert('Please fill out all required fields');
document.getElementById('required_fname').innerHTML = output;
document.getElementById('required_lname').innerHTML = output;
document.getElementById('required_subject').innerHTML = output;
document.getElementById('required_email').innerHTML = output;
document.getElementById('required_message').innerHTML = output;
document.getElementById('required').style.visibility = "visible";
return false;
}
else{
alert('Thanks for contacting me');
result = true;
}

}

联系.php

<?php

if (isset($_POST['submit'])) {
$fname = $_POST['first_name'];
$lname = $_POST['last_name']
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];

$mailTo = "database@terrytowell.com";
$headers = "From: ".$mailFrom;
$name = $fname . " " . $lname;
$txt = "You have received an Email from " . $name . "\n\n" .$message;

mail($mailTo, $subject, $txt, $headers);
header("Location: ../contact.html?mailsend");
}

?>

最佳答案

这几行可能对您有帮助:

首先将此函数插入​​到您的函数旁边

function send_mail($to, $from, $title, $message) {

$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

mail($to, $title, $message, $headers);
}

然后在变量之后调用它

send_mail($_POST['receiver-email'], $_POST['title(maybe website email)'], "title", "here some other post data from your choice"));

关于javascript - HTML 表单无法发送到 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54930438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com