【发布时间】:2016-11-18 14:33:12
【问题描述】:
此表单有效,但在 Wordpress 中无效。我是 Wordpress 的新手,所以我不知道该怎么做。我创建了 4 个文件:
- 我创建表单的页面
- contact.php
- validator.js
- contact.js
这是表单的代码:
<form class="form-horizontal" id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="col-lg-12">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Enter your full name*" required="required" data-error="Full name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-lg-12">
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Enter your email address*" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-lg-12">
<div class="form-group">
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Enter your phone number">
<input type="submit" class="btn modal-btn pull-right" value="send!" />
</div>
</div>
</div>
</form>
这是contact.php中的代码:
<?php
// configure
$from = 'codedstyles.com <hello@codedstyles.com>';
$sendTo = 'Contact Form admin <sanlorena@yahoo.com>';
$subject = 'Hi! You have a new message from a lead';
$fields = array('name' => 'Name', 'phone' => 'Phone', 'email' => 'Email');
// array variable name => Text to appear in email
$okMessage = 'Thank you! We will get in touch with you shortly.';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// Send code
try
{
$emailText = "You have a message to respond\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
【问题讨论】:
-
你是如何包含 js 文件的?而且“不工作”在细节上有点模糊。不提交吗?没有js能用吗?你有任何控制台错误吗? apache/php 日志中的任何内容?
-
您在浏览器控制台中看到任何错误吗?
-
除了提交按钮外,表单上的所有内容都有效。我点击,没有任何反应。我发现它与在操作中添加 php 代码有关,但我只是在学习 php,所以我不知道如何使这项工作......?
-
如果您在浏览器中查看开发人员工具的“网络”选项卡并单击提交按钮,是否会生成新请求?
-
它生成36个请求,第一个是对contact.php表单的工作,只是在Wordpress中不行。我需要找出我需要添加哪些 php 代码以及在哪里添加?