我有一个使用自定义HTML / CSS / JS构建的普通网站。我将网站更改为WordPress网站(并将我的自定义html / css / js变为自定义WordPress主题)。我在网站的主页上有一个联系表格,当我对WordPress进行更改时,该表格中断了。
当表单正常运行时,它会刷新页面并将用户带回到表单(使用anchor id = contactphilly),然后用户会看到确认他们的电子邮件已发送的消息。
我做了很多谷歌搜索,我认为它与表单中的“动作”选项有关,但无论我尝试将其更改为什么,它都不起作用。我尝试了以下操作值:
<?php the_permalink(); ?>/#contactphilly
#contactphilly
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>
$_SERVER['REQUEST_URI']
他们都没有工作。
以下是表单的代码:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'WebKeep.com';
$to = 'info@webkeepteam.com';
$subjectemail = 'Message from WebKeepTeam.com Contact Form ';
$body = "From: $name\n E-Mail: $email\n Subject: $subject\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subjectemail, $body, $from)) {
$result='<div>Thank You! We will respond shortly.</div>';
} else {
$result='<div>Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
和
<form name="contactform" method="post" action="#contactphilly">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<fieldset class="form-group">
<input type="text" value="<?php echo $name;?>" class="form-control transparent-input" id="name" name="name" placeholder="Name">
</fieldset>
<?php echo "<p class='text-danger'>$errName</p>";?>
<fieldset class="form-group">
<input type="email" value="<?php echo $email;?>" class="form-control transparent-input" id="email" name="email" placeholder="Email">
</fieldset>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
<fieldset class="form-group">
<input type="text" class="form-control transparent-input" value="<?php echo $subject;?>" id="subject" name="subject" placeholder="Subject">
</fieldset>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<fieldset class="form-group">
<textarea class="form-control transparent-input" id="message" value="<?php echo $message;?>" name="message" rows="6" placeholder="Message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
</fieldset>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-index pull-right">
</div>
<?php echo $result; ?>
</form>
答案 0 :(得分:0)
我找到了自己问题的答案!我完全忘记了GitHub上的这段代码:https://github.com/dwyl/html-form-send-email-via-google-script-without-server我在网站的其他地方使用它并且它有效,并且它也可以很好地用于页面内的表单!