提交按钮无法在Wordpress中工作

时间:2016-11-18 02:46:15

标签: php html ajax wordpress

此表单有效,但不适用于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'];
}

1 个答案:

答案 0 :(得分:0)

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