【问题标题】:Submit button not working in Wordpress提交按钮在 Wordpress 中不起作用
【发布时间】: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,所以我不知道如何使这项工作......?
    而contact.php文件在根目录下。
  • 如果您在浏览器中查看开发人员工具的“网络”选项卡并单击提交按钮,是否会生成新请求?
  • 它生成36个请求,第一个是对contact.php表单的工作,只是在Wordpress中不行。我需要找出我需要添加哪些 php 代码以及在哪里添加?

标签: php html ajax wordpress


【解决方案1】:
action="contact.php" 

我认为问题在于 contact.php 位置,它看起来可能找不到 php 文件。

在处理 WordPress 时需要使用绝对路径。

根据您上传 php 文件的位置,WordPress 有一些函数可以直接访问 WordPress 根目录或插件目录或主题目录等路径。

<?php $path = get_home_path(); ?>  // root directory
<?php get_plugins( $plugin_folder ) ?> // plugin directory
<?php echo get_template_directory(); ?> // theme directory

如果您需要有关这些功能的更多信息,请查看 WordPress Codex

【讨论】:

  • contact.php文件在根目录下,但是我应该如何添加php代码到action呢?我想到了以下,但它不起作用:
猜你喜欢
  • 2013-04-09
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 2017-03-13
相关资源
最近更新 更多