Php 提交按钮在Wordpress中不工作
这个表单可以使用,但不能在Wordpress中使用。我是Wordpress的新手,所以我不知道该怎么办。我创建了4个文件:Php 提交按钮在Wordpress中不工作,php,html,ajax,wordpress,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">
- 我创建表单的页面
- 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'];
}
我认为问题在于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
//根目录
//插件目录
//主题目录
如果需要有关这些函数的更多信息,请签出
您是如何包含js文件的?“不工作”在细节上有点模糊。它不服从吗?没有js它能工作吗?你有任何控制台错误吗?apache/php日志中有任何内容吗?您在浏览器控制台中看到任何错误吗?表单上的所有内容都正常,但“提交”按钮除外。我点击,什么也没发生。我发现这与实际添加php代码有关,但我只是在学习php,所以我不知道如何让它工作。。。?并且contact.php文件位于根目录中。如果您在浏览器中查看开发者工具的网络选项卡并单击submit按钮,它是否会生成一个新请求?它会生成36个请求,第一个是contact.php表单,而不是Wordpress。我需要找出我需要添加哪些php代码以及在哪里?contact.php文件位于根目录中,但是我应该如何将php代码添加到操作中?我想了一下,但没用:
<?php $path = get_home_path(); ?> // root directory
<?php get_plugins( $plugin_folder ) ?> // plugin directory
<?php echo get_template_directory(); ?> // theme directory