我在几个静态站点上有一个php联系人表单,我已经将表单本身和php代码剪切粘贴到WordPress站点中。
出于某种原因,表单不起作用?WordPress网站上的表单是否需要额外处理(这是一个自定义主题,代码在联系人页面上)。联系方式信息(理论上)在提交时会发送到电子邮件地址。
当我填写并提交表格时,它只会抛出一个404 page error
. php位于页面顶部get_header();
作用
任何提示或建议都将不胜感激。
PHP
<?php
if($_POST[\'submit\']) {
if(!$_POST[\'name\']) {
$error="<br>- Please enter your name";
}
if(!$_POST[\'email\']) {
$error.="<br>- Please enter your email";
}
if(!$_POST[\'telephone\']) {
$error.="<br>- Please enter your telephone number";
}
if(!$_POST[\'message\']) {
$error.="<br>- Please enter your message";
}
if(!$_POST[\'checkbox\']) {
$error.="<br>- Please confirm you agree to the Privacy Policy";
}
if ($error) {
$result=\'<div class="alert error">Whoops, there is an error. Please correct the following: \'.$error.\'</div>\';
} else {
mail("[email protected]", "Contact Message", "Name: ".htmlspecialchars($_POST[\'name\'])."
Email: ".htmlspecialchars($_POST[\'email\'])."
Telephone: ".htmlspecialchars($_POST[\'telephone\'])."
Company: ".htmlspecialchars($_POST[\'company\'])."
Budget: ".htmlspecialchars($_POST[\'budget\'])."
Message: ".htmlspecialchars($_POST[\'message\']),
"From: [email protected]\\r\\n"
);
{
$_POST= array();
$result=\'<div class="alert thankyou" role="alert">THANK YOU! WE\\\'LL BE IN TOUCH SHORTLY...</div>\';
}
$headers = "From: [email protected]\\r\\n";
}
}
?>
HTML
<form id="contactform" method="post" action="#section-2">
<?php echo $result; ?>
<div class="form-row-1 js-st">
<input id="name" type="text" name="name" placeholder="Name" value="<?php echo $_POST[\'name\'];?>">
<input id="email" type="email" name="email" placeholder="Email Address" value="<?php echo $_POST[\'email\'];?>">
</div>
<div class="form-row-2 js-st">
<input id="telno" type="text" name="telephone" placeholder="Telephone Number" value="<?php echo $_POST[\'telephone\'];?>">
<input id="company" type="text" name="company" placeholder="Company [optional]" value="<?php echo $_POST[\'company\'];?>">
</div>
<div class="form-row-3 js-st">
<textarea id="project-details" name="message" placeholder="Tell us about your project"><?php echo $_POST[\'message\'];?></textarea>
</div>
<div class="form-row-4 js-st">
<input id="budget" type="text" name="budget" placeholder="Budget" value="<?php echo $_POST[\'budget\'];?>">
<p class="budget-subline tl ">*We recommend putting in an approximate budget</p>
<input id="privacy-checkbox" type="checkbox" name="checkbox"><label id="privacy-label" for="privacy-checkbox">I agree to the <a href="./privacy.php">privacy policy</a></label>
</div>
<input class="js-st" id="formsubmit" type="submit" name="submit" value="SUBMIT">
</form>
最合适的回答,由SO网友:Chetan Vaghela 整理而成
WordPress使用Name
, email
如果将表单元素名称属性重命名为f-name
和f-email
那么你的问题就会得到解决。我已经在下面的代码中进行了更改,现在它正在工作。
<?php
if($_POST[\'f-submit\']) {
if(!$_POST[\'f-name\']) {
$error="<br>- Please enter your name";
}
if(!$_POST[\'f-email\']) {
$error.="<br>- Please enter your email";
}
if(!$_POST[\'f-telephone\']) {
$error.="<br>- Please enter your telephone number";
}
if(!$_POST[\'f-message\']) {
$error.="<br>- Please enter your message";
}
if(!$_POST[\'f-checkbox\']) {
$error.="<br>- Please confirm you agree to the Privacy Policy";
}
if ($error) {
$result=\'<div class="alert error">Whoops, there is an error. Please correct the following: \'.$error.\'</div>\';
} else {
mail("[email protected]", "Contact Message", "Name: ".htmlspecialchars($_POST[\'f-name\'])."
Email: ".htmlspecialchars($_POST[\'f-email\'])."
Telephone: ".htmlspecialchars($_POST[\'f-telephone\'])."
Company: ".htmlspecialchars($_POST[\'f-company\'])."
Budget: ".htmlspecialchars($_POST[\'f-budget\'])."
Message: ".htmlspecialchars($_POST[\'f-message\']),
"From: [email protected]\\r\\n"
);
{
$_POST= array();
$result=\'<div class="alert thankyou" role="alert">THANK YOU! WE\\\'LL BE IN TOUCH SHORTLY...</div>\';
}
$headers = "From: [email protected]\\r\\n";
}
}
?>
HTML
<form id="contactform" method="post" action="#section-2">
<?php echo $result; ?>
<div class="form-row-1 js-st">
<input id="name" type="text" name="f-name" placeholder="Name" value="<?php echo $_POST[\'f-name\'];?>">
<input id="email" type="email" name="f-email" placeholder="Email Address" value="<?php echo $_POST[\'f-email\'];?>">
</div>
<div class="form-row-2 js-st">
<input id="telno" type="text" name="f-telephone" placeholder="Telephone Number" value="<?php echo $_POST[\'f-telephone\'];?>">
<input id="company" type="text" name="f-company" placeholder="Company [optional]" value="<?php echo $_POST[\'f-company\'];?>">
</div>
<div class="form-row-3 js-st">
<textarea id="project-details" name="f-message" placeholder="Tell us about your project"><?php echo $_POST[\'f-message\'];?></textarea>
</div>
<div class="form-row-4 js-st">
<input id="budget" type="text" name="f-budget" placeholder="Budget" value="<?php echo $_POST[\'f-budget\'];?>">
<p class="budget-subline tl ">*We recommend putting in an approximate budget</p>
<input id="privacy-checkbox" type="checkbox" name="f-checkbox"><label id="privacy-label" for="privacy-checkbox">I agree to the <a href="./privacy.php">privacy policy</a></label>
</div>
<input class="js-st" id="formsubmit" type="submit" name="f-submit" value="SUBMIT">
</form>